为什么在这个代码结束循环中没有得到结束
why in this code end loop is not get end?
我正在字符串中弹出矢量中的数据。但打印后代码没有出来为什么?应该做些什么来使它正确。
#include <iostream>
#include <vector>
using namespace std;
typedef struct add
{
string name;
string address;
}Address;
typedef struct st
{
vector<Address>madder;
}SLL;
int main()
{
SLL * st;
int n=3;
Address ad,rad;
while(n--)
{
cout << "enter the name : ";
cin >> ad.name;
cout << "enter the adderess : ";
cin >> ad.address;
st->madder.push_back(ad);
}
while (!st->madder.empty())
{
rad = st->madder.back();
cout << rad.name << " " <<rad.address <<endl;
st->madder.pop_back();
}
}
在取消引用st
之前,必须分配st
要指向的对象。
此外,您还应该删除已分配的内容。
int main()
{
SLL * st;
int n=3;
Address ad,rad;
st = new SLL; // add this
while(n--)
{
cout << "enter the name : ";
cin >> ad.name;
cout << "enter the adderess : ";
cin >> ad.address;
st->madder.push_back(ad);
}
while (!st->madder.empty())
{
rad = st->madder.back();
cout << rad.name << " " <<rad.address <<endl;
st->madder.pop_back();
}
delete st; // add this
}
另一种选择是不使用指针并直接将SLL
对象分配为变量。
int main()
{
SLL st;
int n=3;
Address ad,rad;
while(n--)
{
cout << "enter the name : ";
cin >> ad.name;
cout << "enter the adderess : ";
cin >> ad.address;
st.madder.push_back(ad);
}
while (!st.madder.empty())
{
rad = st.madder.back();
cout << rad.name << " " <<rad.address <<endl;
st.madder.pop_back();
}
}
相关文章:
- 为什么在这个代码结束循环中没有得到结束
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 使用 shared_ptr 在中断时结束多线程循环
- 如何使用循环结束 c++ 上的行
- 具有迭代器和自定义步长的循环结束条件
- 为什么这个 while 循环在下面的代码中没有结束?
- 为什么在使用不相关的宏时会阻止 while 循环结束?
- 为什么我的 while 循环永远不会在 C++ 中的数字猜测游戏中结束?
- while 循环 C++ 结束,但不继续到其余语句
- 当满足条件时,While循环未结束
- 使用分隔符结束用户输入的循环
- <= 和 == 运算符无法正常工作以结束循环
- C 如何结束一个段循环,该循环添加数字而不使用数字作为触发器来结束循环
- 如果其中一个为真,则 && 结束循环,而不是如果两者都为真
- 在 x 时间后结束循环的性能最低占用方式
- 如果用户对C++说,如何提前结束循环
- C++ 循环条件未结束循环
- 一旦用户错了两次,就结束循环
- EOF和GETCHAR结束循环
- 在sentinel使用条件语句结束循环后显示最大值