注册 登录
编程论坛 VC++/MFC

关于遍历 对象实例 的问题

lanshanlhy 发布于 2012-11-28 16:28, 486 次点击
在使用VB的时候,记得有遍历对象的方法
如:for each object in me,这样能够将me里面的对象全部遍历
再如:class a :有属性 a1,b1
      class b :collection(有两个对象 C1,C2)
      for each tmpa in b
        
       next
在vc++6.0中应该也有这样的功能、可是不知道怎么弄!

3 回复
#2
yuccn2012-11-28 19:18
遍历对象?还是遍历对象的属性?变量对象的属性就没有听过这个说法哦
#3
lanshanlhy2012-12-19 13:36
是我没有描述清楚,当然也是我不知道怎么描述
其实就是迭代器的使用
我是个半路自学学习VC++,学习的时候也很浮躁。所以有此一问
现在我就上个例子给大家参考。以便后来的初学者。
#include<list>
#include<iostream>
using namespace std;

struct mydata{
    int first;
    float second;
};
typedef list<mydata>LISTdata; //定义

void main()
{
    mydata d1,d2,d3;
    d1.first=1;
    d1.second=1.5;
    d2.first=2;
    d2.second=2.5;

    d3.first=3;
    d3.second=3.5;

    LISTdata data1;
 
    data1.insert(data1.begin(),d1);
    data1.insert(data1.end(),d2);
    data1.insert(data1.end(),d3);
    for(list<mydata>::iterator id=data1.begin();id!=data1.end();++id)
        cout<<"first:"<<(*id).first <<" second:"<<(*id).second <<endl;
    //cout<<"first:"<<id->first<<" sencond:"<<id->second <<endl;
}
#4
liao254283012012-12-21 01:36
你是说迭代器吗
1