vector< string>或载体,向量< char> >? [英] vector<string> or vector< vector<char> >?

查看:162
本文介绍了vector< string>或载体,向量< 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 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.

解决方案

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.

这篇关于vector&lt; string&gt;或载体,向量&lt; char&gt; &gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆