访问 {vector of a friendly class} 的 {member of vector}
Accessing a {member of vector} of a {vector of a friendly class}
我想创建一个 class(军队),它由另一个 class(人类)的矢量组成。当试图通过军队访问人类成员时,我 运行 陷入分段错误。
以下代码被简化为必要的:
#include <iostream>
#include <vector>
using namespace std;
class Human {
private:
vector <int> hair;
public:
//The size of the reserved entries is arbitrary.
Human () {hair.reserve(12);}
friend class Army;
int get_hair(int a) {return hair[a];}
};
class Army {
private:
vector <Human> armyVec;
public:
//The size of the reserved entries is arbitrary.
Army () {armyVec.reserve(12);}
Human get_Human(int a) {return armyVec[a];}
};
int main()
{
Human Viktor;
Army Sparta;
cout << Viktor.get_hair(1) << endl; //OK
Viktor = Sparta.get_Human(2);
cout << Viktor.get_hair(1) << endl; //OK
//Now I want to access it directly:
cout << Sparta.get_Human(2).get_hair(1) << endl; //Segfault !!!
cout << "Done" << endl;
return 0;
}
输出为
0
0
Segmentation fault
当 "hair" 不是向量而是整数(相应地发生变化)时有效。如何解决这个问题?
reserve
只保留容量(又名内存)来容纳一些元素。 vector
中没有添加任何元素。您可以使用 resize
添加元素。
这个小程序显示了 reserve
、capacity
、resize
和 size
之间的区别。
#include <iostream>
#include <vector>
int main() {
std::vector<int> a;
a.reserve(10);
std::cout << "Vector capacity after reserve: " << a.capacity() << std::endl;
std::cout << "Vector size after reserve: " << a.size() << std::endl;
a.resize(5);
std::cout << "Vector capacity after resize: " << a.capacity() << std::endl;
std::cout << "Vector size after resize: " << a.size() << std::endl;
return 0;
}
输出:
Vector capacity after reserve: 10
Vector size after reserve: 0
Vector capacity after resize: 10
Vector size after resize: 5
当您知道向量随着时间的推移会增长到一定大小并且您希望在增长时避免多重内存 allocations/copies 时,您可以使用 reserve
。换句话说 - 预先保留容量,可能会提高性能。
当“头发”不是向量而是例如整数(相应地发生变化)时它起作用。如何解决这个问题?
仅当我们使用“static int hair”时,它才在我们使用“int hair”而不是 Vector 时起作用。即使 Human 对象未在 Army 中初始化,“static int”值将始终在 class(此处严格键入)上可用。
我想创建一个 class(军队),它由另一个 class(人类)的矢量组成。当试图通过军队访问人类成员时,我 运行 陷入分段错误。
以下代码被简化为必要的:
#include <iostream>
#include <vector>
using namespace std;
class Human {
private:
vector <int> hair;
public:
//The size of the reserved entries is arbitrary.
Human () {hair.reserve(12);}
friend class Army;
int get_hair(int a) {return hair[a];}
};
class Army {
private:
vector <Human> armyVec;
public:
//The size of the reserved entries is arbitrary.
Army () {armyVec.reserve(12);}
Human get_Human(int a) {return armyVec[a];}
};
int main()
{
Human Viktor;
Army Sparta;
cout << Viktor.get_hair(1) << endl; //OK
Viktor = Sparta.get_Human(2);
cout << Viktor.get_hair(1) << endl; //OK
//Now I want to access it directly:
cout << Sparta.get_Human(2).get_hair(1) << endl; //Segfault !!!
cout << "Done" << endl;
return 0;
}
输出为
0
0
Segmentation fault
当 "hair" 不是向量而是整数(相应地发生变化)时有效。如何解决这个问题?
reserve
只保留容量(又名内存)来容纳一些元素。 vector
中没有添加任何元素。您可以使用 resize
添加元素。
这个小程序显示了 reserve
、capacity
、resize
和 size
之间的区别。
#include <iostream>
#include <vector>
int main() {
std::vector<int> a;
a.reserve(10);
std::cout << "Vector capacity after reserve: " << a.capacity() << std::endl;
std::cout << "Vector size after reserve: " << a.size() << std::endl;
a.resize(5);
std::cout << "Vector capacity after resize: " << a.capacity() << std::endl;
std::cout << "Vector size after resize: " << a.size() << std::endl;
return 0;
}
输出:
Vector capacity after reserve: 10
Vector size after reserve: 0
Vector capacity after resize: 10
Vector size after resize: 5
当您知道向量随着时间的推移会增长到一定大小并且您希望在增长时避免多重内存 allocations/copies 时,您可以使用 reserve
。换句话说 - 预先保留容量,可能会提高性能。
当“头发”不是向量而是例如整数(相应地发生变化)时它起作用。如何解决这个问题?
仅当我们使用“static int hair”时,它才在我们使用“int hair”而不是 Vector 时起作用。即使 Human 对象未在 Army 中初始化,“static int”值将始终在 class(此处严格键入)上可用。