【问题标题】:Converting a Vector of Characters to a Vector of Strings in C++在 C++ 中将字符向量转换为字符串向量
【发布时间】:2020-02-27 11:00:26
【问题描述】:

对于以下problem,我需要返回一个字符串向量,其中向量中的每个元素的长度为 1。我有一个生成字符向量的答案。我认为它可以满足我的需要,但我不确定如何将结果作为长度为 1 的字符串向量返回。请参阅下面的实现。注意,我不能改变函数的返回类型,它必须是一个字符串向量。鉴于我在解决方案中对字符进行操作,我不确定如何更改它。

    vector<string> commonChars(vector<string>& A) {
        vector<char> resVec;
        unordered_map<char, int> mapObj;

        for (const auto& str : A) {
            for (const auto& letter : str) {
                mapObj[letter]++;
            }
        }

        int sizeOfInput = A.size();
        for (int i{}; i < A[0].size(); i++) {
            if (!mapObj.count(A[0][i])) continue;
            resVec.insert(resVec.begin(), mapObj[(A[i])] / sizeOfInput, A[i]); 
        }
        return resVec;
    }

【问题讨论】:

标签: c++ string vector char


【解决方案1】:
vector<string> ret;
for(char ch: resVec)
    ret.push_back(string(1,ch));
return ret;

这是将std::vector&lt;char&gt; 转换为std::vector&lt;std::string&gt; 的简单方法。 有兴趣的也可以看看:Convert a single character to a string?

简而言之,string(1,ch) 是做什么的实际转换。

【讨论】:

    【解决方案2】:
    std::vector<char> resVec;
    ...
    std::vector<std::string> returnVec;
    returnVec.reserve(resVec.size());
    for (char ch : resVec) {
        returnVec.push_back(std::string(1, ch));
        // or: returnVec.emplace_back(1, ch);
    }
    return returnVec;
    

    或者:

    std::vector<char> resVec;
    ...
    std::vector<std::string> returnVec;
    returnVec.reserve(resVec.size());
    std::transform(resVec.begin(), resVec.end(), std::back_inserter(returnVec), 
        [](char ch){ return std::string(1, ch); }
    );
    return returnVec;
    

    或者:

    std::vector<char> resVec;
    ...
    std::vector<std::string> returnVec(resVec.size());
    std::transform(resVec.begin(), resVec.end(), returnVec.begin(), 
        [](char ch){ return std::string(1, ch); }
    );
    return returnVec;
    

    也就是说,您可以完全消除 std::vector&lt;char&gt; 并直接从 std::map 数据中填充最终的 std::vector&lt;std::string&gt;

    std::vector<std::string> commonChars(std::vector<std::string>& A) {
        std::vector<std::string> resVec;
        std::unordered_map<char, int> mapObj;
    
        for (const auto& str : A) {
            for (const auto& letter : str) {
                mapObj[letter]++;
            }
        }
    
        size_t sizeOfInput = A.size();
        if (sizeOfInput > 0) {
            for (const auto& letter : A[0]) {
                auto iter = mapObj.find(letter);
                if (iter != mapObj.end()) {
                    resVec.insert(resVec.begin(), iter->second / sizeOfInput, std::string(1, letter));
                }
            }
        }
    
        return resVec;
    }
    

    【讨论】:

      【解决方案3】:

      解决问题的方法不同。您可以计算每个字符串在向量中每个字母 a-z 的出现次数,并循环遍历每个向量,添加一个字符在所有字符串中出现的最小次数:

      vector<string> commonChars(vector<string>& A) {
          const int maxTimes=100;
          vector<string> resVec;
          vector<vector<int>> counts;
          counts.reserve(A.size());
      
          //Count occurrences of letters in each string
          for (const auto& word : A) {
              vector<int> count(26, 0);
              for (const auto letter : word) {
                  count[letter-'a']++;
              }
              counts.push_back(count);
          }
      
          //Add minimum number of times a letter occurs in all strings
          for (int i=0; i<26; ++i)
          {
              int min=maxTimes;
              for (auto &count : counts)
              {
                  if (count[i] < min) {
                      min = count[i];
                  }
              }
      
              for (int j=0; j<min; ++j) {                 
                  resVec.push_back(string(1, i+'a'));
              }
          }
      
          return resVec;
      }
      

      注意:string(1, i+'a') 从单个字符(值:i+'a')创建大小为 1 的字符串。

      【讨论】:

      • 很好的答案!谢谢你。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 2021-09-19
      相关资源
      最近更新 更多