C++向结构问题指针数组添加元素
所以我有一个名为Person的结构: 结构人{ 字符*名称; 金钱; 国际货币基金组织; }; 我这样做是为了让我的main读取一个名为gift1.in的文件,其中包含:C++向结构问题指针数组添加元素,c++,arrays,pointers,struct,C++,Arrays,Pointers,Struct,所以我有一个名为Person的结构: 结构人{ 字符*名称; 金钱; 国际货币基金组织; }; 我这样做是为了让我的main读取一个名为gift1.in的文件,其中包含: 5 dave laura owen vick amr 还有一点。重要的是这些线。 第一行代表有多少人,然后是其他人的名字。 正如您可能猜到的,这些名称将成为Person结构中的char*名称 现在来看问题: int main { 结构Person*peopleArray[maxFriendsum]; int指数=0; int
5
dave
laura
owen
vick
amr
还有一点。重要的是这些线。
第一行代表有多少人,然后是其他人的名字。
正如您可能猜到的,这些名称将成为Person结构中的char*名称
现在来看问题:
int main
{
结构Person*peopleArray[maxFriendsum];
int指数=0;
int maxIndex=0;
std::流式输出1.out;
标准::ifstream fin gift1.in;
//获得最大数量的名字!
fin>>最大索引;
//为每个名字创建一个人。。。
对于索引=0;maxIndex!=索引;索引++{
char newName[MAXNAMECHAR+1];
fin>>新名称;
std::cout您一遍又一遍地为Person.name指定相同的指针,这就是为什么它们都只接受您对原始地址所做的更改 要改变这种行为,您必须为每个条目创建一个新实例,例如使用strcpy 在那之后,你得到了你期望的行为 此外,您还可以使用std::string和std::vector使用一些节省的方法:
咬紧牙关,只使用C++约定。你的问题是在这里对NeNEnter进行删除。你在堆栈上声明它,在局部范围内,它是for循环的,而且它很可能每次都有相同的地址。所以,在你读的文件的最后一个条目中,AMR然后被分配给那个地址。然后你复制那个PO。在makePerson中插入arount,并将其分配给该结构中的char*name元素
for (index = 0; maxIndex != index; index++) {
char newName[MAXNAMECHAR+1];
makePerson(peopleArray, index, newName);
}
您可以通过为名称分配内存来解决此问题:
char * newName = new char[MAXNAMECHAR+1];
但是你也必须清理自己,删除内存。开始使用真实的C++,避免所有的Maloc垃圾。
// Create a person for every name...
for (index = 0; maxIndex != index; index++) {
char newName[MAXNAMECHAR+1];
在这里,您在堆栈上分配内存以存储名称。您没有告诉编译器每个循环需要一个这样的内存,因此您将在每次迭代中分配/释放堆栈空间,或者编译器将其视为:
char newName[MAXNAMECHAR+1];
for (index ... ) {
...
您读取的每个名称都会进入此变量,该变量很可能位于内存/堆栈中的同一位置。您可以将此内存的地址传递给create函数:
makePerson(peopleArray, index, newName);
然后将该地址存储在Person对象中,因此它们都指向相同的地址—堆栈上的临时位置
您应该考虑亲自制作一个STD::字符串或使名称复制它正在通过的字符串。
struct Person {
std::string name;
int initialMoney;
int currentMoney;
Person(const char* name_) : initialMoney(0), currentMoney(0) {}
};
而makePerson变成了
记住,你必须删除这些对象,就像你应该在它们完成之前释放它们一样,在考虑STD::UnjyQupTp
这是一个C++实现:
#include <string>
#include <vector>
#include <iostream>
using std::cout;
using std::cin;
using std::vector;
using std::string;
struct Person {
string m_name;
int m_initialMoney;
int m_currentMoney;
Person(const std::string& name_, int money_=0)
: m_name(name_)
, m_initialMoney(0), m_currentMoney(0)
{}
};
int main ()
{
vector<Person> people;
// Obtain max number of names!
size_t maxIndex = 0;
cin >> maxIndex;
if (maxIndex <= 0) {
cout << "Nothing to do.\n";
return 1;
}
// Create a person for every name...
for (size_t index = 0; maxIndex != index; index++) {
std::string newName;
cin >> newName;
cout << "Inserting " << people.size() << ": " << newName << std::endl;
people.emplace_back(newName);
}
// Check to see if the person's name is being read correctly
cout << "Checking if the proper person's name is there: \n";
for (auto& person : people) {
cout << "Reading " << person.m_name << std::endl;
}
cout << "Changing a single element of an array: \n";
people[1].m_name = "John";
for (auto& person : people) {
cout << "Reading " << person.m_name << std::endl;
}
return 0;
}
最大的帮助是将所有的char **转换为STD::string和所有数组到STD::vector……你正在用C++编写,所以你应该更喜欢新的Malc。得到它!谢谢你的回复!非常彻底,我很喜欢它,谢谢你指点我使用合适的C++库以及我当前C的解决方案。颂歌也!
struct Person {
std::string name;
int initialMoney;
int currentMoney;
Person(const char* name_) : initialMoney(0), currentMoney(0) {}
};
void makePerson(Person *peopleArray[], int insertIndex, char *name)
{
peopleArray[insertIndex] = new Person(name);
}
#include <string>
#include <vector>
#include <iostream>
using std::cout;
using std::cin;
using std::vector;
using std::string;
struct Person {
string m_name;
int m_initialMoney;
int m_currentMoney;
Person(const std::string& name_, int money_=0)
: m_name(name_)
, m_initialMoney(0), m_currentMoney(0)
{}
};
int main ()
{
vector<Person> people;
// Obtain max number of names!
size_t maxIndex = 0;
cin >> maxIndex;
if (maxIndex <= 0) {
cout << "Nothing to do.\n";
return 1;
}
// Create a person for every name...
for (size_t index = 0; maxIndex != index; index++) {
std::string newName;
cin >> newName;
cout << "Inserting " << people.size() << ": " << newName << std::endl;
people.emplace_back(newName);
}
// Check to see if the person's name is being read correctly
cout << "Checking if the proper person's name is there: \n";
for (auto& person : people) {
cout << "Reading " << person.m_name << std::endl;
}
cout << "Changing a single element of an array: \n";
people[1].m_name = "John";
for (auto& person : people) {
cout << "Reading " << person.m_name << std::endl;
}
return 0;
}