c++ - 使用对象和动态数组(使用指针创建)或向量时 for 循环不起作用
问题描述
主体中的第二个循环仅在一步后停止。我不知道为什么!我的代码{
#include <iostream>
#include <string>
#include <vector> // using dynamic array
using namespace std;
int i; // for loops
class person{
private:
string name; // the name of the person
vector < string > ne; // the names that are in the list
int mn_f; // the money at the first before giving to other people
int n; // the number of people in the list
int mn_l = 0; // the money at the end of the party
public:
void set_name(string name){ // setting the name
this -> name = name;
}
string get_name(){ // returning the name
return name;
}
void set_mn_f(int mn_f){ // getting the first money of the person
this -> mn_f = mn_f;
}
void set_n(int n){ // getting the n
this -> n = n;
}
void set_list(){ // getting the people that are in the list
string a; // a member in the list
for( i = 0; i < n; i++){
cin >> a;
ne.push_back(a);
}
//cout << "started\n";
}
void get_list(){
for( i = 0; i < n; i++){
cout << i + 1 << ' ' << ne[i] << endl;
}
//cout << "finished\n";
}
void mod(){
mn_l += (mn_f % n); // the mod of money to the n is for the person
}
void set_mn_l(int n){ // calculating the last money of the person
mn_l += n;
}
int get_mn_l(){
return mn_l; // returning the last money of the person
}
~person(){
ne.clear(); // deleting ne
}
};
int main(){
int n; // the number of attendees
string ne; // name of the people
cin >> n;
person Person; // the person and its information
vector < person > a; // the list of the people in the party
int mn_f; // the money at the first before giving to other people
int nu; // the number of people in the list
for( i = 1; i <= n; i++){
cin >> ne; // entering the names for the first time
}
for( i = 0; i < n; i++){ // entering the names for the second time and completing the information
cin >> ne >> mn_f >> nu;
Person.set_name(ne); // name
Person.set_mn_f(mn_f); // money at first
Person.set_n(nu); // the number of the people in the list of Person
Person.set_list(); // entering the people in the list
a.push_back(Person); // putting the Person in the list
cout << "loop\n";
}
cout << a.size() << endl;
for( i = 0; i < n; i++){
Person = a[i];
cout << Person.get_name() << endl;
Person.get_list();
cout << "loop2\n";
}
cout << a.size() << endl;
a.clear(); // deleting a
return 0;
}
解决方案
推荐阅读
- c# - 在 Windows 11 上找不到任何已安装的 .NET Core SDK(Visual Studio 代码)
- bash - 如何在另一个命令变量中获取命令变量?
- discord.py - 任何人都知道为什么我的解禁命令不起作用?| 不和谐.py
- scala - 通过在另一个数据框中的所有行上迭代数据框中的一行来检查最小值
- python - 从列表中删除重复值 (Python)
- excel - 更改 2 个单元格的值和相应的公式值需要复制到不同的工作表中
- python-3.x - 没有调用handle_client
- sql - 在子查询中联合所有
- slider - 关于滑块革命的在线广播流
- php - 如何正确实现这种场景?