c++ - 找到一堆元素的位置

标签 c++ string performance vector location

我需要找到位置 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;
            }
        }
    }
}

或任何更快的替代方法? 告诉我你的代码,因为我是编程新手,很难理解你的解释,谢谢。

最佳答案

您正在将 intstring 进行比较,两者的比较并不相等。你必须将一个转换为另一个

// 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/

相关文章:

ios - 为什么这段代码解压缩 UIImage 比天真的方法好得多?

performance - 在 React 单页面应用程序中,测量页面在视觉上看起来完整的时间的有用方法是什么?

c++ - 编译并运行Qt程序

python - if 语句不适用于抓取的网页文本

c - 查找字符串是否为大小写混合的最有效方法

Python 3 的 input() 返回一个与相同 str 文字不同的对象的 str

javascript - 同位素过滤是否可以很好地处理数百个项目和多个类别?

c++ - openMP lastprivate 和 firstprivate 到同一个变量

c++ - 如何访问 std::list<string> 中的某个元素

c++ - 类构造函数: Incomplete type is not allowed (in member list) - VS C++ w/QT