对向量使用push_back< vector< string> > [英] Using push_back on a vector<vector<string> >

查看:359
本文介绍了对向量使用push_back< vector< string> >的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点尴尬,这样一个简单的问题阻碍了我,但经过几个小时无果的谷歌,我仍然卡住。



为了简化我的问题,此第二行崩溃:

  vector< vector< string> > sorted_words; 
sorted_words [0] .push_back(hello);

不应 sorted_words [0] 一个空的向量,我可以合法push_back到?

解决方案

不。您有一个向量的向量。你没有向第二行添加任何东西,所以 sorted_words [0] sorted_words ,还不存在。



您尝试将hello推送到一个空的向量



空指针解引用



我会问你真的想要一个向量



如果你想要一个字符串的向量,请使用:



<$> p $ p> vector< string> sorted_words;
sorted_words.push_back(hello);

如果你确实需要一个向量(字符串)的向量,

 矢量< string> first_vector; 
first_vector.push_back(hello);
sorted_words.push_back(first_vector);


I'm somewhat embarrassed that such a simple problem has stymied me, but after a few hours of fruitless googling, I'm still stuck.

To simplify my problem, the 2nd line of this crashes:

  vector<vector<string> > sorted_words;
  sorted_words[0].push_back("hello");

Shouldn't sorted_words[0] represent an empty vector that I can legally push_back onto?

解决方案

No. You have a vector of vectors. You haven't added anything to either vector by line 2, so sorted_words[0], the first element in sorted_words, doesn't exist yet.

You're trying to push "hello" into a null vector.

Null pointer dereference!

I would ask "do you really want a vector of vectors, or just a vector of strings"?

If you want a vector of strings, then use:

vector<string> sorted_words;
sorted_words.push_back("hello");

If you really do want a vector of vectors (of strings), then use:

vector<string> first_vector;
first_vector.push_back("hello");
sorted_words.push_back(first_vector);

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

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