将具有字符串成员的结构对象推入std::vector时出错 STD::复制< /COD>。非常感谢你。我需要读一本合适的C++书,它讨论了哪些C代码不可用。@ Acthb.:加速C++。它实际上并没有讨论C代码不使用的内容,而是教C++使用它的方式(C++上的一个完全罕见的特性)。当存在函数时,不会有人从栈中弹出,然后*人就会空了吗?不,它不会。拷贝正在进行。所以即使 THEMANE/COD>超出了范围, *In人< /C>仍然是有效的,因为它是一个拷贝。非常感谢-我觉得我需要适当的C++教育。 struct Person { string Name; string Lastname; int Age; Person(string Name, string Last, int Age) : Name(Name), LastName(Last), Age(Age) {} }; std:::vector<Person> people; people.push_back(Person("Zarah", "Petros", 13)); people.push_back(Person("Tina", "Yarroos", 26)); std::ostream &operator<<(std::ostream &os, Person const &p) { return os << "Name: " << p.Name < "\n" << "Last: " << p.LastName << "\n" << "Age: " << p.Age << "\n"; } for (int i=0; i<people.size(); i++) std::cout << people[i] << "\n"; std:copy(people.begin(), people.end(), std::ostream_iterator<Person>(std::cout, "\n")); for (auto &p : people) std::cout << p << "\n"; #include <string> #include <iostream> #include <vector> using std::string; struct Person { string Name; string LastName; int Age; Person(string Name, string Last, int Age) : Name(Name), LastName(Last), Age(Age) {} }; std::ostream &operator<<(std::ostream &os, Person const &p) { return os << "Name: " << p.Name << "\n" << "Last: " << p.LastName << "\n" << "Age: " << p.Age << "\n"; } int main(){ std::vector<Person> people; people.push_back(Person("Zarah", "Petros", 13)); people.push_back(Person("Tina", "Yarroos", 26)); for (auto &p : people) std::cout << p << "\n"; return 0; }
不要在非POD类型上使用将具有字符串成员的结构对象推入std::vector时出错 STD::复制< /COD>。非常感谢你。我需要读一本合适的C++书,它讨论了哪些C代码不可用。@ Acthb.:加速C++。它实际上并没有讨论C代码不使用的内容,而是教C++使用它的方式(C++上的一个完全罕见的特性)。当存在函数时,不会有人从栈中弹出,然后*人就会空了吗?不,它不会。拷贝正在进行。所以即使 THEMANE/COD>超出了范围, *In人< /C>仍然是有效的,因为它是一个拷贝。非常感谢-我觉得我需要适当的C++教育。 struct Person { string Name; string Lastname; int Age; Person(string Name, string Last, int Age) : Name(Name), LastName(Last), Age(Age) {} }; std:::vector<Person> people; people.push_back(Person("Zarah", "Petros", 13)); people.push_back(Person("Tina", "Yarroos", 26)); std::ostream &operator<<(std::ostream &os, Person const &p) { return os << "Name: " << p.Name < "\n" << "Last: " << p.LastName << "\n" << "Age: " << p.Age << "\n"; } for (int i=0; i<people.size(); i++) std::cout << people[i] << "\n"; std:copy(people.begin(), people.end(), std::ostream_iterator<Person>(std::cout, "\n")); for (auto &p : people) std::cout << p << "\n"; #include <string> #include <iostream> #include <vector> using std::string; struct Person { string Name; string LastName; int Age; Person(string Name, string Last, int Age) : Name(Name), LastName(Last), Age(Age) {} }; std::ostream &operator<<(std::ostream &os, Person const &p) { return os << "Name: " << p.Name << "\n" << "Last: " << p.LastName << "\n" << "Age: " << p.Age << "\n"; } int main(){ std::vector<Person> people; people.push_back(Person("Zarah", "Petros", 13)); people.push_back(Person("Tina", "Yarroos", 26)); for (auto &p : people) std::cout << p << "\n"; return 0; },c++,string,vector,struct,C++,String,Vector,Struct,不要在非POD类型上使用memcpy()。它不调用复制构造函数 改用std::copy() 在这种情况下,做作业更容易。替换: memcpy(in_person, &t_person, sizeof(t_person)); 与 string是一个类,不应由memcpy操作复制。它可能保存实例特定的数据,如果由两个不同的实例保存,这些数据将被置乱(这可能是问题的原因) 想象一下,std::string类似于: class string { private: char * data
memcpy()。它不调用复制构造函数
改用std::copy()
在这种情况下,做作业更容易。替换:
memcpy(in_person, &t_person, sizeof(t_person));
与
string是一个类,不应由memcpy操作复制。它可能保存实例特定的数据,如果由两个不同的实例保存,这些数据将被置乱(这可能是问题的原因)
想象一下,std::string类似于:
class string
{
private:
char * data;
int dataLength;
};
如果将一个字符串memcpy到另一个字符串,则数据和数据长度都会复制到另一个位置(最近被视为普通字符串实例)。但是,当对这些字符串调用析构函数时(当它们超出范围时),它们将尝试释放data
字段中保存的数据。第一个字符串(该指针的实际所有者)将释放该指针指向的内存。但是,复制字符串的另一个析构函数将运行,并将再次尝试释放此内存,这是不允许的
请注意,这正是您的系统所报告的:双重释放内存
你的代码非常C风格。在C++中,你会用构造函数创建一个类,而不是函数,它填充了一个结构。我将用以下方式编写您的代码:
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
struct Person
{
public:
string Name;
string Lastname;
int Age;
Person(string newName, string newLastname, int newAge)
: Name(newName), Lastname(newLastname), Age(newAge)
{
}
};
int main(int argc, char *argv[])
{
vector<Person> people;
Person person1("Zareh", "Petros", 13);
people.push_back(person1);
Person person2("Tina", "Yarros", 26);
people.push_back(person2);
for(unsigned int i=0; i < people.size() ; i++)
{
cout << "Element - " << i << endl;
cout << "name:" << people[i].Name << endl;
cout << "lastname:" << people[i].Lastname << endl;
cout << "age:" << people[i].Age << endl;
}
getchar();
return 0;
}
你可以简单地写:
struct Name { ... };
您已经被告知在这种情况下不应该使用memcpy
,所以我不想重复了
您的CreatePerson
的问题远远超出了使用memcpy
的范围,仅仅更改为std::copy
并不能真正解决问题
您几乎可以肯定的是,您应该以构造函数的形式编写该功能,而不是使用自由函数来创建person:
struct Person {
string Name;
string Lastname;
int Age;
Person(string Name, string Last, int Age)
: Name(Name), LastName(Last), Age(Age)
{}
};
这样,我们可以更干净地创建Person对象:
std:::vector<Person> people;
people.push_back(Person("Zarah", "Petros", 13));
people.push_back(Person("Tina", "Yarroos", 26));
这样,您的主流代码就可以将完整的Person
对象插入到流中,而不必关注Person
包含的内容或显示方式的内部细节:
for (int i=0; i<people.size(); i++)
std::cout << people[i] << "\n";
for(inti=0;iYou正在混合C(memcpy
)和C++(STL
)这就造成了混乱。使用代码< > STD::复制< /COD>。非常感谢你。我需要读一本合适的C++书,它讨论了哪些C代码不可用。@ Acthb.:加速C++。它实际上并没有讨论C代码不使用的内容,而是教C++使用它的方式(C++上的一个完全罕见的特性)。当存在函数时,不会有人从栈中弹出,然后*人就会空了吗?不,它不会。拷贝正在进行。所以即使 THEMANE/COD>超出了范围, *In人< /C>仍然是有效的,因为它是一个拷贝。非常感谢-我觉得我需要适当的C++教育。
struct Person {
string Name;
string Lastname;
int Age;
Person(string Name, string Last, int Age)
: Name(Name), LastName(Last), Age(Age)
{}
};
std:::vector<Person> people;
people.push_back(Person("Zarah", "Petros", 13));
people.push_back(Person("Tina", "Yarroos", 26));
std::ostream &operator<<(std::ostream &os, Person const &p) {
return os << "Name: " << p.Name < "\n"
<< "Last: " << p.LastName << "\n"
<< "Age: " << p.Age << "\n";
}
for (int i=0; i<people.size(); i++)
std::cout << people[i] << "\n";
std:copy(people.begin(), people.end(),
std::ostream_iterator<Person>(std::cout, "\n"));
for (auto &p : people)
std::cout << p << "\n";
#include <string>
#include <iostream>
#include <vector>
using std::string;
struct Person {
string Name;
string LastName;
int Age;
Person(string Name, string Last, int Age)
: Name(Name), LastName(Last), Age(Age)
{}
};
std::ostream &operator<<(std::ostream &os, Person const &p) {
return os << "Name: " << p.Name << "\n"
<< "Last: " << p.LastName << "\n"
<< "Age: " << p.Age << "\n";
}
int main(){
std::vector<Person> people;
people.push_back(Person("Zarah", "Petros", 13));
people.push_back(Person("Tina", "Yarroos", 26));
for (auto &p : people)
std::cout << p << "\n";
return 0;
}