使用指针正确操作向量。

huangapple go评论71阅读模式
英文:

Using pointer to vector properly

问题

我在我的IDE中有一个指向向量的指针设置,但我不认为我做得对,因为每次尝试将数据push_back到它时,程序都会在代码的那一点崩溃。例如:

std::vector<int> a;
std::vector<std::vector<int>>* a1;
a.push_back(3);
a1->push_back(a); // 程序在这里崩溃,执行之前没有任何错误提示。

我看到这个站点的其他用户有初始化的向量指针,但现在我想起来,我不记得他们真的访问它们来放数据或以后进行编辑。也许这就是我卡在这一点上经常崩溃的原因。所以有人可以解释一下为什么会发生这种情况以及如何正确使用向量指针吗?我想像这样访问向量a1:

cout << "值为:" << (*a1)[0][0] + 4 << endl; // 当程序运行时,我甚至无法到达这一步

我哪里弄错了?

英文:

I have a pointer to vector setup in my IDE but i dont think im doing it right cuz everytime i try to push_back data into it the program crashed at that point in the code. <br/>
For example:

std::vector&lt;int&gt; a;  std::vector&lt;std::vector&lt;int&gt; &gt; *a1;
a.push_back(3);
a1-&gt;push_back(a); // Right here the program crashes with no indication prior to execution that there was an error.

I've seen some other users of this site have pointers of vectors initialized but now that i think about it i dont recall them really accessing them to put data in or to edit later. Maybe thats why im stuck at this point getting Crashes. So could anyone explain why it and happens how to do pointer to vectors properly. I want to access vector a1 like so: <br/>

cout&lt;&lt;&quot; value is: &quot;&lt;&lt;(*a1)[0][0] + 4 &lt;&lt;endl; // I cant even reach this far when the program runs

What about this im not getting right?

答案1

得分: 3

a 是一个向量。以下是 a 的声明(和定义):

std::vector<int> a;

a1 是一个指向向量的指针。以下是 a1 的声明和定义,但是请注意,指针没有指向一个已定义的位置(感谢 @user4581301 指出):

std::vector<std::vector<int>> *a1;

为了定义 a1 中的值,可以将地址分配给一个已经存在的向量,或者通过 new 分配一个新的向量,例如:

// 预定义向量
std::vector<std::vector<int>> a;

// 将 a 的地址分配给 a1
std::vector<std::vector<int>> *a1 = &a;

// 或者分配一个新的向量
std::vector<std::vector<int>> *a2 = new std::vector<std::vector<int>>;

上述代码的目的是展示如何获取对象的地址,可以通过 & 的地址运算符或 new 运算符来实现。

关于“最佳实践”,我建议避免使用指针,而是使用引用。但当需要分配内存时,可以:

  • 将指针包装在 unique_ptrshared_ptr 对象中(保证在发生未捕获异常时释放分配的内存块),例如:
// 分配一个单一的向量<int>,并插入值 1、2、3
auto my_vec_ptr = std::make_unique<std::vector<int>>(1, 2, 3);
// (*my_vec_ptr).push_back(42); // 解引用指针并添加值 42

// 分配一个包含 5 个向量<int> 的数组
auto my_vec_array = std::make_unique<std::vector<int>[]>(5);
// my_vec_array[0].push_back(42); // 向第一个向量添加 42
  • 或者使用容器,如 vector、map 等,例如:
// 使用默认插入方式构造包含 5 个默认实例的向量<int>
std::vector<std::vector<int>> my_vec_of_vec{5};
// my_vec_of_vec[0].push_back(42); // 向第一个向量添加 42
// my_vec_of_vec.push_back(std::vector<int>{1,2,3}); // 向列表中添加一个新的向量
英文:

a is a vector. Below is a declaration (and definition) of a:

std::vector&lt;int&gt; a;

a1 is a pointer to a vector. Below is a declaration and definition of a1, but the pointer doesn't point at a defined location (thanks to @user4581301, for pointing that out):

std::vector&lt;std::vector&lt;int&gt; &gt; *a1;

In order to define the value in a1, one could either assign the address of an already existing vector or allocate a new vector via new, e.g.

//pre defined vector
std::vector&lt;std::vector&lt;int&gt;&gt; a;

//assign address of a to a1
std::vector&lt;std::vector&lt;int&gt;&gt; *a1 = &amp;a;

//or allocate a new vector
std::vector&lt;std::vector&lt;int&gt;&gt; *a2 = new std::vector&lt;std::vector&lt;int&gt;&gt;;

The intent of the above code was to show how an address of an object could be obtained, either via the address of &amp; or the new operator.

When it comes to 'best practice', i would say, avoid pointers use references. But when an allocation is needed, then

  • one could wrap the pointer either into an unique_ptr or shared_ptr object (guaranteed deallocation of the allocated memory block, e.g. in case of an uncaught exception)

e.g.

//allocate a single vector&lt;int&gt; and insert the values 1, 2, 3
auto my_vec_ptr = std::make_unique&lt;std::vector&lt;int&gt;&gt;(1, 2, 3);
//(*my_vec_ptr).push_back(42); //dereference pointer and add the value 42

//allocates an array of 5 vector&lt;int&gt;
auto my_vec_array = std::make_unique&lt;std::vector&lt;int&gt;[]&gt;(5);
//my_vec_array[0].push_back(42); //adds 42 to the first vector
  • or use a container, like vector, map, etc.

e.g.

//constructs the vector with 5 default-inserted instances of vector&lt;int&gt;
std::vector&lt;std::vector&lt;int&gt;&gt; my_vec_of_vec{5};
//my_vec_of_vec[0].push_back(42); //adds 42 to the first vector
//my_vec_of_vec.push_back(std::vector&lt;int&gt;{1,2,3}); //add a new vector to the list

huangapple
  • 本文由 发表于 2023年2月24日 03:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定