[英]vector<string> or vector< vector<char> >?
I'm working on a Boggle game solver that would read lines of a text file (the board). 我正在研究一个可以读取文本文件(电路板)行的Boggle游戏求解器。
I've been going back and forth if I should use a vector
of string
s or a vector
matrix of char
s. 我已经来回我是否应该使用vector
的string
S或A vector
的矩阵char
秒。 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. 我认为chars
的vector
将更容易访问,因为它将是myvec[y][x]
,其中string
s的vector
将要求我使用string.at()
函数。
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. 如果我应该将每一行解析为char
或者将该行保留为string
并根据需要访问每个char
,我不知道哪个会有更好的性能。 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. 至于评论,你可以使用operator[]
的字符串,就像vector
S或阵列。
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. 在适当的优化下,对于vector<char>
和string
的性能大致相同 - 两者都是引擎盖下的数组,并且在两种情况下, operator[]
实际上将是结构成员访问和间接查找。 They even provide almost the same set of methods . 他们甚至提供了几乎相同的方法 。
Choose whichever makes your code more readable/simple. 选择哪个使您的代码更易读/更简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.