C++ 运算符不匹配操作数错误
c++ operator doesn't match operand error
我是C 的新手。当我尝试输出字符串向量的结果时,我会遇到错误。我希望有人可以吗?GenerateCombinations功能的代码来自https://www.programmingalgorithms.com/algorithm/nique-combinations。我写了主()函数。我正在使用vs社区2015。
#include "stdafx.h"
#include <iostream>
#include <Vector>
#include <string>
using namespace std;
//*****Please include following header files***** /
// string
// vector
/***********************************************/
/*****Please use following namespaces*****/
// std
/*****************************************/
static vector<vector<string>> GenerateCombinations(vector<string> arr)
{
vector<vector<string>> combinations;
int length = arr.size();
for (int i = 0; i < (1 << length); ++i)
{
vector<string> combination;
int count = 0;
for (count = 0; count < length; ++count)
{
if ((i & 1 << count) > 0)
combination.push_back(arr[count]);
}
if (count > 0 && combination.size() > 0) {
combinations.push_back(combination);
}
}
return combinations;
}
int main() {
vector<string> arr = { "How", "Are", "You" };
vector<vector<string>> combinations = GenerateCombinations(arr);
vector <string> ::iterator itr;
for (itr = combinations.begin(); itr < combinations.end(); itr++)
{
cout << *itr << endl;
}
正如@SAM在评论中指出的那样,您正在尝试将std::vector<std::vector<std::string>>::iterator
从combinations.begin()
分配到std::vector<std::string>::iterator
,因此是不匹配的。
解决问题的最简单方法是不必担心实际类型,然后使用auto
:
for (auto itr = combinations.begin(); itr < combinations.end(); ++itr)
或更简单:
for (auto combination : combinations)
在这里combination
是std::vector<std::string>
,因此您不能仅仅打印,您也需要迭代:
for (auto combination : combinations)
{
for (auto c : combination)
{
std::cout << c << ' ';
}
std::cout << "n";
}
相关文章:
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 错误:二进制'operator*' 'float'和'float[0]'类型的操作数无效
- 错误 C2679 二进制"<<":未找到采用类型为 'T' 的右侧操作数的运算符
- 错误:'operator>'不匹配(操作数类型为 'Student' 和 'Student'
- 如何摆脱逗号运算符的这个错误右操作数没有效果( wunsued 值)?
- c++ 错误:二进制"运算符+"类型"矩阵*"和"矩阵*"的操作数无效
- 使用堆栈编译错误的 C++ 反向行没有运算符匹配操作数
- 当我尝试构建此程序时,出现此错误:需要左键作为赋值的左操作数
- 错误:二进制">>":未找到采用类型为"const char [1] 的右操作数的运算符
- C++ 重载:[错误] 与'operator='不匹配(操作数类型为 'String' 和"字符串")
- 错误 C2679:二进制'<<':找不到采用类型 'overloaded-function' 的右侧操作数的运算符(或者没有可接受的转换)
- 我收到一个错误无效的操作数,类型为 const char [42] 和二进制"运算符+"的双倍数
- 错误:与"运算符>>"不匹配(操作数类型为"std::istream
- 错误:C2679 二进制"==":未找到采用类型 'const std::string' 的右侧操作数的运算符(或者没有可接受的转换)
- 错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|
- 错误:"->"的基本操作数具有非指针类型"const"
- 使用英特尔线程构建块编译:错误操作数为?:
- 错误:操作数不匹配
- C++错误(操作数类型为"std::string 和 'void")