vector<string> or vector< vector<char> >?

梦想与她 提交于 2019-12-23 16:53:39

问题


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 strings or a vector matrix of chars. I'm thinking the vector of chars would be easier to access as it would be myvec[y][x] where the vector of strings 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 chars 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.


回答1:


As commented, you can use operator[] on strings just like vectors 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.



来源:https://stackoverflow.com/questions/10491445/vectorstring-or-vector-vectorchar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!