C++ 如何通过函数将元素保存在向量中
C++ How to Save Elements in a Vector from a Function
考虑以下代码。在 list1
中,我将保存在堆上的元素添加到矢量并打印。它按预期工作。在 list2
中,我从一个函数向向量添加元素,该函数也在堆上分配元素。
我知道我必须在 addNode
中的堆上分配 Node
,否则它会在函数 returns 时被释放。然而,通过最后的打印语句,我可以看到堆上的节点仍然被分配,但它们没有出现在我的向量中。
#include <iostream>
#include <vector>
using namespace std;
/*
Simple node class for demo
*/
class Node
{
public:
string val;
Node(string value) { this->val = value; }
};
Node *addNode(vector<Node *> list)
{
// allocate space for node on the heap so it isn't destroyed after function returns
auto node = new Node("foo");
// add pointer to node onto vector
list.push_back(node);
return node;
}
/*
Simple function for printing vector contents
*/
template <typename T>
void printVector(T d)
{
cout << "Vector has size " << d.size() << " and elements: ";
for (auto p = d.begin(); p < d.end(); p++)
{
cout << (*p)->val << ",";
}
cout << "\n";
}
int main()
{
// make a new vector
vector<Node *> list1;
// add elements allocated on the heap
list1.push_back(new Node("foo"));
list1.push_back(new Node("foo"));
list1.push_back(new Node("foo"));
printVector(list1); // prints: "Vector has size 3 and elements: foo,foo,foo,"
// make a new vector
vector<Node *> list2;
// add elements allocated on the heap from a function
addNode(list2);
addNode(list2);
// save one of the nodes to a variable for demonstration
auto node = addNode(list2);
printVector(list2); // prints: "Vector has size 0 and elements:"
cout << node->val << "\n"; // prints: "foo"
return 0;
}
谁能解释一下如何从函数向向量添加元素?
在 addNode()
中,您正在按值 传递 vector
,因此 copy 调用者的vector
制作完成,然后将 Node*
添加到副本中,原始 vector
不受影响。
您需要通过引用 vector
传递 而不是:
Node* addNode(vector<Node*> &list)
同printVector()
。您正在按值传递 vector
,只是没有修改 copy,但您仍应按 (const) 引用传递 vector
以避免创建完全复制:
template <typename T>
void printVector(const T &d)
附带说明一下,您正在泄露您创建的 Node
。使用完毕后,您需要 delete
它们:
int main()
{
vector<Node*> list1;
list1.push_back(new Node("foo"));
list1.push_back(new Node("foo"));
list1.push_back(new Node("foo"));
printVector(list1);
for(auto *p : list1)
delete p;
vector<Node*> list2;
addNode(list2);
addNode(list2);
auto node = addNode(list2);
printVector(list2);
cout << node->val << "\n";
for(auto *p : list2)
delete p;
return 0;
}
最好使用 std::unique_ptr
为您管理:
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
...
Node* addNode(vector<unique_ptr<Node>> &list)
{
list.push_back(make_unique<Node>("foo"));
return list.back().get();
}
template <typename T>
void printVector(const T &d)
{
cout << "Vector has size " << d.size() << " and elements: ";
for (const auto &p : d)
{
cout << p->val << ",";
}
cout << "\n";
}
int main()
{
vector<unique_ptr<Node>> list1;
list1.push_back(make_unique<Node>("foo"));
list1.push_back(make_unique<Node>("foo"));
list1.push_back(make_unique<Node>("foo"));
printVector(list1);
vector<unique_ptr<Node>> list2;
addNode(list2);
addNode(list2);
auto node = addNode(list2);
printVector(list2);
cout << node->val << "\n";
return 0;
}
考虑以下代码。在 list1
中,我将保存在堆上的元素添加到矢量并打印。它按预期工作。在 list2
中,我从一个函数向向量添加元素,该函数也在堆上分配元素。
我知道我必须在 addNode
中的堆上分配 Node
,否则它会在函数 returns 时被释放。然而,通过最后的打印语句,我可以看到堆上的节点仍然被分配,但它们没有出现在我的向量中。
#include <iostream>
#include <vector>
using namespace std;
/*
Simple node class for demo
*/
class Node
{
public:
string val;
Node(string value) { this->val = value; }
};
Node *addNode(vector<Node *> list)
{
// allocate space for node on the heap so it isn't destroyed after function returns
auto node = new Node("foo");
// add pointer to node onto vector
list.push_back(node);
return node;
}
/*
Simple function for printing vector contents
*/
template <typename T>
void printVector(T d)
{
cout << "Vector has size " << d.size() << " and elements: ";
for (auto p = d.begin(); p < d.end(); p++)
{
cout << (*p)->val << ",";
}
cout << "\n";
}
int main()
{
// make a new vector
vector<Node *> list1;
// add elements allocated on the heap
list1.push_back(new Node("foo"));
list1.push_back(new Node("foo"));
list1.push_back(new Node("foo"));
printVector(list1); // prints: "Vector has size 3 and elements: foo,foo,foo,"
// make a new vector
vector<Node *> list2;
// add elements allocated on the heap from a function
addNode(list2);
addNode(list2);
// save one of the nodes to a variable for demonstration
auto node = addNode(list2);
printVector(list2); // prints: "Vector has size 0 and elements:"
cout << node->val << "\n"; // prints: "foo"
return 0;
}
谁能解释一下如何从函数向向量添加元素?
在 addNode()
中,您正在按值 传递 vector
,因此 copy 调用者的vector
制作完成,然后将 Node*
添加到副本中,原始 vector
不受影响。
您需要通过引用 vector
传递 而不是:
Node* addNode(vector<Node*> &list)
同printVector()
。您正在按值传递 vector
,只是没有修改 copy,但您仍应按 (const) 引用传递 vector
以避免创建完全复制:
template <typename T>
void printVector(const T &d)
附带说明一下,您正在泄露您创建的 Node
。使用完毕后,您需要 delete
它们:
int main()
{
vector<Node*> list1;
list1.push_back(new Node("foo"));
list1.push_back(new Node("foo"));
list1.push_back(new Node("foo"));
printVector(list1);
for(auto *p : list1)
delete p;
vector<Node*> list2;
addNode(list2);
addNode(list2);
auto node = addNode(list2);
printVector(list2);
cout << node->val << "\n";
for(auto *p : list2)
delete p;
return 0;
}
最好使用 std::unique_ptr
为您管理:
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
...
Node* addNode(vector<unique_ptr<Node>> &list)
{
list.push_back(make_unique<Node>("foo"));
return list.back().get();
}
template <typename T>
void printVector(const T &d)
{
cout << "Vector has size " << d.size() << " and elements: ";
for (const auto &p : d)
{
cout << p->val << ",";
}
cout << "\n";
}
int main()
{
vector<unique_ptr<Node>> list1;
list1.push_back(make_unique<Node>("foo"));
list1.push_back(make_unique<Node>("foo"));
list1.push_back(make_unique<Node>("foo"));
printVector(list1);
vector<unique_ptr<Node>> list2;
addNode(list2);
addNode(list2);
auto node = addNode(list2);
printVector(list2);
cout << node->val << "\n";
return 0;
}