【问题标题】:Is there any way to use std::cout when calling a function within a class?在类中调用函数时有什么方法可以使用 std::cout 吗?
【发布时间】:2022-11-15 10:10:47
【问题描述】:
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>

class Solution {
public:
    std::vector<std::vector<std::string>> groupAna(std::vector<std::string> strs) {
        std::unordered_map<std::string, std::vector<std::string>> ana;
        for (int i {0}; i < strs.size(); ++i)
        {
            std::string key = getKey(strs[i]);
            ana[key].push_back(strs[i]);
        }
        
        std::vector<std::vector<std::string>> results;
        for (auto it = ana.begin(); it != ana.end(); ++it)
        {
            results.push_back(it->second);
        }
        
//        for (int i {0}; i < results.size(); ++i)
//        {
//            for (int j {0}; j < results[i].size(); ++j)
//            {
//                std::cout << results[i][j] << " ";
//            }
//        }
        return results;
    }
    
private:
    std::string getKey(std::string str) {
        std::vector<int> count(26);
        for (int i {0}; i < str.length(); ++i)
        {
            ++count[str[i] - 'a'];
        }
        
        std::string key {""};
        for (int j {0}; j < 26; ++j)
        {
            key.append(std::to_string(count[j] + 'a'));
        }
        
        return key;
    }
};

int main() {
    std::vector<std::string> strs ({"eat","tea","tan","ate","nat","bat"});
    Solution obj;
    std::cout << obj.groupAna(strs);
    
    return 0;
}

我收到此错误:Invalid operands to binary expression ('std::ostream' (aka 'basic_ostream&lt;char&gt;') and 'std::vector&lt;std::vector&lt;std::string&gt;&gt;' (aka 'vector&lt;vector&lt;basic_string&lt;char, char_traits&lt;char&gt;, allocator&lt;char&gt;&gt;&gt;&gt;'))

这个解决方案适用于 Leetcode 上的Group Anagrams,我只是使用 XCode 来练习写出所有需要的代码,而不是使用 Leetcode 提供的代码。我的问题是在解决方案类中调用并尝试打印 groupAna 函数时出现的。我相信这个错误告诉我我想要打印的东西不是你可以打印的,但不知道这是否完全正确。

我最终试图在其各自的向量中打印每个字符串。被注释掉的是一个解决方法,它给了我我想要的东西,但它没有显示向量中的每个单词所以我怎么能知道它是否在向量中它应该在除了它以正确的顺序之外你知道?

输出为bat tan nat eat tea ate

【问题讨论】:

  • 您可以使用 fmt 库打印出标准容器。 Demo
  • std::cout &lt;&lt; obj.groupAna(strs); 将不起作用,因为该函数返回 std::vector&lt;std::vector&lt;std::string&gt;&gt;。一般来说,vectors 没有 operator&lt;&lt; 重载。

标签: c++ algorithm data-structures


【解决方案1】:

成员函数groupAna 返回std::vector&lt;std::vector&lt;std::string&gt;&gt;,但std::vectors 没有operator&lt;&lt; 重载。您可以改为输出到std::ostringstream,然后返回std::string

#include <sstream>

class Solution {
public:
    // now returns a std::string instead:
    std::string groupAna(const std::vector<std::string>& strs) {
        std::unordered_map<std::string, std::vector<std::string>> ana;
        for (size_t i{0}; i < strs.size(); ++i) {
            std::string key = getKey(strs[i]);
            ana[key].push_back(strs[i]);
        }

        std::vector<std::vector<std::string>> results;
        for (auto it = ana.begin(); it != ana.end(); ++it) {
            results.push_back(it->second);
        }

        std::ostringstream os; // used to build the output string
        for (int i{0}; i < results.size(); ++i) {
            for (int j{0}; j < results[i].size(); ++j) {
                os << results[i][j] << ' ';
            }
        }
        /* or using a range based for-loop:
        for(const auto& inner : results) {
            for(const std::string& str : inner) {
                os << str << ' ';
            }
        }
        */
        return os.str(); // return the resulting string
    }
// ...
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2019-04-01
    • 1970-01-01
    相关资源
    最近更新 更多