find() 没有返回可以使用的 int 值,并且在编译之前给出错误
find() is not returning an int value that can be used and is giving an error before compiling
>我正在尝试获取创建的新向量的索引值,但是find((函数不允许我为变量分配所述find((的返回值
我尝试在其他地方分配值,摆弄起点和终点,但程序根本不会用 find(( 的值分配 tempind。
void count_city(vector<string> city, vector<string> state) {
vector<string> cities(4);
vector<int> counted(4);
string temp = " ";
int tempind = 0;
for (int i = 0; i < city.size(); i++) {
temp = city.at(i);
if (find(cities.begin(), cities.end(), temp) != cities.end()) {
continue;
}
else {
cities.push_back(temp);
}
tempind = (find(cities.begin(), cities.end(), temp));
counted.at(tempind) = count(city.begin(), city.end(), temp);
}
}
我只是希望 tempind 成为矢量城市中城市的索引,但它不允许我编译并给我一个错误,指出我的类型不同。
std::find()
返回迭代器,而不是索引。 如果需要索引,可以将迭代器传递给std::distance()
:
auto found = find(cities.begin(), cities.end(), temp);
tempind = distance(cities.begin(), found);
但在这种情况下,您根本不需要find()
索引。由于push_back()
插入向量的末尾,因此所需的索引是push_back()
向量之前的大小:
for (int i = 0; i < city.size(); i++) {
temp = city[i];
if (find(cities.begin(), cities.end(), temp) != cities.end()) {
continue;
}
tempind = cities.size();
cities.push_back(temp);
counted.at(tempind) = count(city.begin(), city.end(), temp);
}
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?