vector< string>或载体,向量< char> >? [英] vector<string> or vector< vector<char> >?
问题描述
我正在使用 Boggle 游戏求解器读取文本文件的行(董事会)。
如果我应该使用向量 s或
向量
矩阵。我认为的
向量将更容易访问,因为它将是
myvec [y] [x]
其中向量的
string
string.at()
函数。
我不知道哪一个会有更好的性能,如果我应该将每行解析为 char
,或将该行保留为 string
并访问每个 char
。任何建议,我应该做哪一个?
如您所说,您可以使用 c>
在适当的优化下,运算符[] ,向量< char>
的性能大致相同,因为 string
并且在这两种情况下 operator []
将有效地是结构成员访问和间接查找。他们甚至提供了几乎同一组的方法。
选择使代码更易读/ / p>
I'm working on a Boggle game solver that would read lines of a text file (the board).
I've been going back and forth if I should use a vector
of string
s or a vector
matrix of char
s. I'm thinking the vector
of chars
would be easier to access as it would be myvec[y][x]
where the vector
of string
s would require me to use the string.at()
function.
I don't know which would have a better performance if I should parse each line in to the char
s or leave the line as a string
and access each char
as needed. Any suggestions on which one I should do? Explaining why would be helpful.
As commented, you can use operator[]
on strings just like vector
s or arrays.
Under proper optimisation, the performace will be about the same for vector<char>
as for string
- both are arrays under the hood, and in both cases operator[]
will effectively be a structure member access and an indirect lookup. They even provide almost the same set of methods.
Choose whichever makes your code more readable/simple.
这篇关于vector< string>或载体,向量< char> >?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!