C++ 中的二维字符串

发布于 2024-08-29 07:45:30 字数 890 浏览 3 评论 0 原文

我想用 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
.....#..............
.....#..............
.....#..............
.....#..............
######..............
.......###..........
.......#.#..........
.......###...#######
.............#.....#
.............#######

它给了我分段错误。为什么 ?怎么了 ?应该如何做才能正常工作?谢谢。

I want to write something like 2d strings in C++.
I tried with :

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";
        }

no compile errors, but when i enter input like:

10 20
.....#..............
.....#..............
.....#..............
.....#..............
######..............
.......###..........
.......#.#..........
.......###...#######
.............#.....#
.............#######

It gives me segmentation fault. Why ? What's wrong ? And how it should be done so it would work correctly ? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

风月客 2024-09-05 07:45:30

这个问题似乎暗示所需的数据结构是一组 n 行,每行包含 m 个字符。有两种方法可以将其视为 nxm 字符矩阵,或作为 n m 字符向量(字符串也类似)但与 vector 不同)。

所以看来你不想要一个向量向量字符串,你想要一个向量 char向量,或者只是string向量

无论如何,您必须在使用 table[i][j] 或(稍微更惯用的 c++ 之前分配适当的空间量,但在这种情况下没有必要,因为 mn< /code> 事先已知)使用类似 push_back 的内容添加到末尾。

另请注意,cin>>sstdin 读取整个(这使得 vector > 我认为解决方案更容易处理)。

The question seems to imply that the data structure needed is a set of n lines with m characters each. There are two ways to think of this -- as an nxm char matrix, or as n m-character vectors (and a string is similar but not identical to vector<char>).

So it seems you don't want a vector of vectors of strings, you want either a vector of vectors of chars, or just a vector of strings.

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 and n are known beforehand) use something like push_back to add to the end.

Note also that the cin>>s reads an entire line from stdin (which makes the vector<string> solution a bit easier to deal with, I think).

单身狗的梦 2024-09-05 07:45:30

当向 vector 中插入新内容时,您不能只按索引分配 - 您需要使用 push_back 方法或类似的方法。

    for(i=0;i<n;i++) {
            vector<string> row;
            for(j=0;j<m;j++) {
                    cin>>s;
                    row.push_back(s);
            }
            table.push_back(row);
    }

When inserting something new into a vector, you can't just allocate by index - you need to use the push_back method or something similar.

    for(i=0;i<n;i++) {
            vector<string> row;
            for(j=0;j<m;j++) {
                    cin>>s;
                    row.push_back(s);
            }
            table.push_back(row);
    }
紫﹏色ふ单纯 2024-09-05 07:45:30

vector 类有一个接受大小和元素的 ctor,因此:

vector< vector<char> > table(ROW_COUNT, vector<char>(COLUMN_COUNT, '.'));

将初始化一个向量,其中包含作为第二个参数传递的向量的 ROW_COUNT 副本,其中包含 COLUMN_COUNT.

The vector class has a ctor that takes the size and an element, so:

vector< vector<char> > table(ROW_COUNT, vector<char>(COLUMN_COUNT, '.'));

would initialize a vector containing ROW_COUNT copies of the vector passed as the second argument, which contains COLUMN_COUNT times ..

蓝戈者 2024-09-05 07:45:30

对尚未插入的向量进行索引是错误的。

因此,举一个更简单的例子:

std::vector<int> v;
v[0] = 3;//bad, v[0] doesn't exist yet

正确:

std::vector<int> v;
v.push_back(3);
int x = v[0];//ok

但在你的情况下,你首先插入一个向量,然后将整个向量推入另一个向量。

It is an error to index a vector that you haven't inserted into yet.

So taking a more easy example:

std::vector<int> v;
v[0] = 3;//bad, v[0] doesn't exist yet

Correct:

std::vector<int> v;
v.push_back(3);
int x = v[0];//ok

But in your case you are first inserting into a vector and then pushing the whole vector into the other vector.

北城孤痞 2024-09-05 07:45:30

我认为 vector > 会更适合您的目的。

I think a vector<vector<char> > would be better suitable for your purposes.

樱娆 2024-09-05 07:45:30

您的向量仍然是空的,而且您可能不想这样做。您可以使用 push_back() 附加值,或者使用合适的大小初始化它们:

std::cin >> n >> m;
typedef std::vector<std::string> StringVec;
std::vector<StringVec> table(n, StringVec(m));

正如其他人提到的,std::string 可能不是您真正需要的。

You vectors 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:

std::cin >> n >> m;
typedef std::vector<std::string> StringVec;
std::vector<StringVec> table(n, StringVec(m));

As others mentioned though, std::string might not be what you really need.

逐鹿 2024-09-05 07:45:30

您需要使用 char s; cin.get(s); 而不是 string s; cin>>s;,因为 cin>>s 将一次读取整个单词(在您的示例中,这相当于一整行)。

此外,您需要采取一些措施来确保每个数组的大小正确。在您的代码中,您有一个由 0 个字符串组成的向量组成的向量。例如:

vector< vector<string> > table;
int m,n,i,j;
char s;
cin>>n>>m;
for(i=0;i<n;i++) {
    table.push_back(vector<string>());
        for(j=0;j<m;j++) {
                cin.get(s);
                table[i].push_back(string(1,s));
        }
}
cout << "\n\n\n\n";
for(i=0;i<n;i++) {
        for(j=0;j<m;j++) {
                cout<<table[i][j]<<" ";
        }
        cout<<"\n";
}

You need to use char s; cin.get(s); instead of string s; cin>>s;, becuase cin>>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:

vector< vector<string> > table;
int m,n,i,j;
char s;
cin>>n>>m;
for(i=0;i<n;i++) {
    table.push_back(vector<string>());
        for(j=0;j<m;j++) {
                cin.get(s);
                table[i].push_back(string(1,s));
        }
}
cout << "\n\n\n\n";
for(i=0;i<n;i++) {
        for(j=0;j<m;j++) {
                cout<<table[i][j]<<" ";
        }
        cout<<"\n";
}
情绪操控生活 2024-09-05 07:45:30

您必须创建元素向量,要添加元素,您必须使用 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 write tables.insert(tables.end(), v)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文