在类中调用函数时是否有任何方法使用std::cout ?


#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;
}

我收到这个错误:

二进制表达式的无效操作数('std::ostream'(又名'basic_ostream')和'std::vector'(又名'vector>'))

此解决方案适用于Leetcode上的组字谜。我只是用XCode练习写出所有需要的代码,而不是使用Leetcode给出的。

我的问题出现在调用和试图打印Solution类中的groupAna()函数时。我相信错误是告诉我我想打印的东西不是你可以打印的东西,但我不知道这是否完全正确。

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

输出bat tan nat eat tea ate

成员函数groupAna返回std::vector<std::vector<std::string>>,但std::vector没有operator<<重载。您可以输出到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 (size_t i{0}; i < results.size(); ++i) {
for (size_t 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
}
// ...
};

Leetcode会要求你实现一个给定的函数,他们会用一组单元测试来测试你的代码,为每个测试提供一个输入,并期望得到一个输出。也就是说,您不需要在groupAnagrams函数中打印字符串向量的向量的向量,而只需正确地生成该矩阵。

无论如何,如果您想测试您的代码,并打印groupAnagrams的输出,您可以使用fmt库。<fmt/ranges.h>头文件允许您打印标准容器。对于向量,它将把它们打印在方括号中。对于您的示例,请注意您将拥有内部向量。

(演示)

#include <fmt/ranges.h>
int main() {
std::vector<std::string> strs({"eat", "tea", "tan", "ate", "nat", "bat"});
Solution obj;
fmt::print("{}", obj.groupAna(strs));
}
// Outputs: [["bat"], ["tan", "nat"], ["eat", "tea", "ate"]]

或者,如果你想平坦化这个结构,除了Ted Lyngmo提出的两种解决方案(流和两个嵌套的基于范围的for循环),你可以使用c++ 23 range。

(演示)

#include <fmt/core.h>
#include <ranges>
int main() {
std::vector<std::string> strs({"eat", "tea", "tan", "ate", "nat", "bat"});
Solution obj;
auto&& groups{ obj.groupAna(strs) };
for (auto&& str : (groups | std::views::join)) {
fmt::print("{} ", str);
}
}
// Outputs: bat tan nat eat tea ate

最新更新