我需要找到位置 1 2 3 4 5 6 7 8 9 10,它们存储为名为“line”的字符串并保存在名为“info”的字符串 vector 中。但是我的代码在我的代码中没有任何错误?
string line;
vector<vector<string>>info;
for (int x = 0; x < info.size(); x++)
{
for (int y = 0; y < info[x].size(); y++)
{
for (int i = 0; i <= 10; i++)
{
if (info[x][y] == i)
{
cout << "row " << x;
cout << "column " << y;
}
}
}
}
或任何更快的替代方法? 告诉我你的代码,因为我是编程新手,很难理解你的解释,谢谢。
最佳答案
您正在将 int
与 string
进行比较,两者的比较并不相等。你必须将一个转换为另一个
// compare them as strings
if (info[x][y] == std::to_string(i))
或
// compare them as ints
if (std::stoi(info[x][y]) == i)
关于c++ - 找到一堆元素的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34067045/