[英]C++ how to work with pointers to vector pointers
我有这个设计:
vector<string*>* tmp = new vector<string*>;
如何将元素放入其中? 我不明白如何将这个向量放入几个字符串。 我尝试过这个
std::string srt = "abc";
tmp->push_back(srt);
但是编译器诅咒并且语法不正确,我不知道该怎么办
只是不要变得过于指针式(假设这是一个相对简单的程序)
vector<string> tmp;
std::string srt = "abc";
tmp.push_back(srt);
当然,指向字符串的指针的向量是一场等待发生的灾难。 而且可能您也不需要指向向量的指针
语法为tmp->push_back(&srt);
但是最好使用vector<string>
因为vector将为您处理指针。
首选:
std::vector<std::string> tmp;
std::string srt{"abc"};
tmp.push_back(srt);
要回答您的问题:
如何使用指向矢量指针的指针?
让我们看一个基本的例子...
#include <iostream>
#include <string>
#include <vector>
int main() {
// has to be initialized since it is a pointer.
// However, this will compile but will fail at runtime.
std::vector<std::string*>* pvWords = nullptr;
std::string word1 = "Hello";
std::string word2 = "World";
// Here you need the `operator->()` from the vector
// and since it will store pointers to strings,
// you can just pass in the address of...
pvWords->push_back( &word1 );
pvWords->push_back( &word2 );
for ( auto& s : *pvWords ) // here we need to dereference pvWords
std::cout << *s << ' '; // and here we need to dereference s
std::cout << '\n';
return 0;
}
-预期的输出-
Hello World
但是,这将编译但在运行时失败,并在Visual Studio中导致访问冲突错误。 之所以失败,是因为一旦strings
指针超出范围,它们就会被销毁,并且vector
或vector*
试图引用的内存现在无效。 您也许可以通过使用动态内存来克服此问题,但是即使那样,它也只会使代码变得更加复杂且难以管理,并可能导致更多看不见的错误。
如果我们将以上内容修复为以下代码段,则代码将起作用:
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string*>* pvWords = new std::vector<std::string*>;
std::string word1 = "Hello";
std::string word2 = "World";
pvWords->push_back( &word1 );
pvWords->push_back( &word2 );
for ( auto& s : (*pvWords) )
std::cout << (*s) << ' ';
std::cout << '\n';
delete pvWords; // don't forget to cleanup dynamic memory.
return 0;
}
这将与动态内存一起使用,并将提供以下输出:
Hello World
但是,为避免所有这些错误和代码复杂性,只需不使用任何指针,尤其是在使用std::string
。
第一个解决方案可以工作,但是如果任何指针在使用之前变得无效,并且会避免运行时错误,则很棘手。
在这种情况下,无需使用指针。 您不会在需要继承或多态性的地方存储基类的派生类。
由于std::vector
和std::string
的行为,只需这样做:
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> vWords;
vWords.push_back( "Hello" );
vWords.push_back( "World" );
for ( auto& s : vWords )
std::cout << s << ' ';
std::cout << '\n';
return 0;
}
由于std::vector
和std::string
管理它们自己的内存,因此将产生相同的预期结果,而不会进行任何动态内存分配和内存清理,没有任何错误,也没有任何代码复杂性。
-输出-
Hello World
此版本的代码更容易阅读,更易于使用和调试,更易于其他人使用,并且将完全按照您的期望执行。 此解决方案非常简单,快速且高效,无所有麻烦。
在没有令人信服的理由的情况下,可以但不应该以一种方式使用向量,但我无法想到一个。
vector<string*>* tmp = new vector<string*>;
这是您必须执行的操作:
using std::vector; using std::string; using std::cout;
int main()
{
vector<string*>* tmp = new vector<string*>;
string srt = "abc";
(*tmp).push_back(&srt);
cout << *(*tmp)[0] << endl;
delete tmp;
}
或者,可以使用间接运算符->
代替*
。 他们做同样的事情,但是在不同的地方看起来都很丑。
tmp->push_back(&srt);
cout << *tmp->operator[](0) << endl;
让我们检查*(*tmp)[0]
发生了什么。 (*tmp)
是由向量指向的tmp
。 (*tmp)[0]
是向量中第一个元素的内容,它是指向字符串的指针。 *(*tmp)[0]
是向量中第一个元素指向的字符串。
除了有些晦涩的语法外,还要提防向量中超出范围的元素所指向的字符串,这些字符串会创建悬挂的指针。
这是使用容器存储字符串的规范方法。
int main()
{
vector<string> tmp;
string srt = "abc";
tmp.push_back(srt);
cout << tmp[0] << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.