您的位置:首页 > 编程语言 > C语言/C++

c++积累(1):一个简单的list容器迭代程序

2014-06-12 14:51 537 查看
#include <iostream>
#include <list>
#include <string>
/*
#include <opencv2/core/affine.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
*/

using namespace std;
//using namespace cv;

struct students
{
int age;
string name;
void clear()
{
age = 0;
name = "Unknown";
}
};

int main()
{
students temp;
list<students>studentObj;
studentObj.clear();
int z_count = 0;
int z_age;
string z_name;

cin.get();

while(z_count < 4)
{
cout << "Please input age: ";
cin >> z_age;
cout << endl;
cout << "Please input name: ";
cin >> z_name;

temp.age = z_age;
temp.name = z_name;
studentObj.push_back(temp);
memset(&temp, 0, sizeof(students));
z_count++;
}

cout << "Type Enter, go on!";
cin.get();

int size = studentObj.size();
cout << endl;

list<students>::iterator zhang;
for (zhang = studentObj.begin(); zhang != studentObj.end(); zhang++)
{
temp.clear();
temp = *zhang;//看来迭代器和指针都是一样的,存储的是地址
cout << "age: " << temp.age << ", name: " << temp.name << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: