C++ 中的二维字符串
我想用 C++ 写一些类似于二维字符串的东西。 我尝试使用:
vector< vector<string> > table;
int m,n,i,j;
string s;
cin>>n>>m;
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
cin>>s;
table[i][j] = s;
}
}
cout << "\n\n\n\n";
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
cout<<table[i][j]<<" ";
}
cout<<"\n";
}
没有编译错误,但是当我输入如下输入时:
10 20
.....#..............
.....#..............
.....#..............
.....#..............
######..............
.......###..........
.......#.#..........
.......###...#######
.............#.....#
.............#######
它给了我分段错误。为什么 ?怎么了 ?应该如何做才能正常工作?谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这个问题似乎暗示所需的数据结构是一组
n
行,每行包含m
个字符。有两种方法可以将其视为nxm
字符矩阵,或作为n
m
字符向量(字符串也类似)但与vector
不同)。所以看来你不想要一个
向量
的向量
的字符串
,你想要一个向量
char
的向量
,或者只是string
的向量
。无论如何,您必须在使用 table[i][j] 或(稍微更惯用的 c++ 之前分配适当的空间量,但在这种情况下没有必要,因为
m
和n< /code> 事先已知)使用类似
push_back
的内容添加到末尾。另请注意,
cin>>s
从stdin
读取整个行(这使得vector
> 我认为解决方案更容易处理)。The question seems to imply that the data structure needed is a set of
n
lines withm
characters each. There are two ways to think of this -- as annxm
char matrix, or asn
m
-character vectors (and a string is similar but not identical tovector<char>
).So it seems you don't want a
vector
ofvector
s ofstring
s, you want either avector
ofvector
s ofchar
s, or just avector
ofstring
s.In any event, you have to allocate the appropriate amount of space before using table[i][j] or (slightly more idiomatic c++, but not necessary in this case since
m
andn
are known beforehand) use something likepush_back
to add to the end.Note also that the
cin>>s
reads an entire line fromstdin
(which makes thevector<string>
solution a bit easier to deal with, I think).当向
vector
中插入新内容时,您不能只按索引分配 - 您需要使用push_back
方法或类似的方法。When inserting something new into a
vector
, you can't just allocate by index - you need to use thepush_back
method or something similar.vector
类有一个接受大小和元素的 ctor,因此:将初始化一个向量,其中包含作为第二个参数传递的向量的
ROW_COUNT
副本,其中包含COLUMN_COUNT
次.
。The
vector
class has a ctor that takes the size and an element, so:would initialize a vector containing
ROW_COUNT
copies of the vector passed as the second argument, which containsCOLUMN_COUNT
times.
.对尚未插入的向量进行索引是错误的。
因此,举一个更简单的例子:
正确:
但在你的情况下,你首先插入一个向量,然后将整个向量推入另一个向量。
It is an error to index a vector that you haven't inserted into yet.
So taking a more easy example:
Correct:
But in your case you are first inserting into a vector and then pushing the whole vector into the other vector.
我认为
vector >
会更适合您的目的。I think a
vector<vector<char> >
would be better suitable for your purposes.您的
向量
仍然是空的,而且您可能不想这样做。您可以使用push_back()
附加值,或者使用合适的大小初始化它们:正如其他人提到的,
std::string
可能不是您真正需要的。You
vector
s are still empty and you probably wan't to . You can either append values using e.g.push_back()
or init them with a suitable size:As others mentioned though,
std::string
might not be what you really need.您需要使用
char s; cin.get(s);
而不是string s; cin>>s;
,因为cin>>s
将一次读取整个单词(在您的示例中,这相当于一整行)。此外,您需要采取一些措施来确保每个数组的大小正确。在您的代码中,您有一个由 0 个字符串组成的向量组成的向量。例如:
You need to use
char s; cin.get(s);
instead ofstring s; cin>>s;
, becuasecin>>s
will read a whole word (in your sample this is equivalent to a whole line) at a time.Additionally, you need to do something to get the size of each array right. In your code you have a vector of 0 vectors of 0 strings. For example:
您必须创建元素向量,要添加元素,您必须使用 v.insert(v.end(), s) 。最后(当你有完整的行数据时)。您必须编写
tables.insert(tables.end(), v)
You must create vector of elements, to add elements you must use
v.insert(v.end(), s)
. and finnaly (when you have full row data). you must writetables.insert(tables.end(), v)