ITPub博客

首页 > IT职业 > IT生活 > test

test

原创 IT生活 作者:Tom 时间:2009-08-18 16:55:07 0 删除 编辑

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include
#include
//670147918
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

class Test
{
friend bool operator < (Test const& t1, Test const& t2)
{
return t1.a < t2.a;
}

friend ostream& operator<<(ostream& o, Test const& t)
{
o< return o;
}

public:
int a;
int b;
int c;
};


typedef list INTLIST;

//从前向后显示list队列的全部元素

void put_list(INTLIST list, char *name)
{
INTLIST::iterator plist;

printf("The contents of %sn", name);
for(plist = list.begin(); plist != list.end(); plist++)
{
printf("a = %d, b = %d n", ((Test*)&(*plist))->a, ((Test*)&(*plist))->b);
}
printf("n");
}


int main(int argc, char* argv[])
{

//list1对象初始为空
INTLIST list1;
//list2对象最初有10个值为6的元素
Test test1;
test1.a = 110;
test1.b = 2;
test1.c = 3;

Test test2;
test2.a = 120;
test2.b = 20;
test2.c = 30;

Test test4;
test4.a = 130;
test4.b = 20;
test4.c = 30;

INTLIST list2(10, test1);
//list3对象最初有3个值为6的元素
INTLIST list3(list2.begin(),--list2.end());

//声明一个名为i的双向迭代器
INTLIST::iterator i;

//从前向后显示各list对象的元素
//put_list(list1,"list1");
//put_list(list2,"list2");
//put_list(list3,"list3");


//从list1序列后面添加两个元素
list1.push_back(test1);
list1.push_back(test2);
list1.push_back(test4);
//put_list(list1,"list1");

//list1.push_front(5);
//list1.push_front(7);
//printf("list1.push_front(5) and list1.push_front(7)n");
//put_list(list1,"list1");

//在list1序列中间插入数据
Test test3;
test3.a = 199;
test3.b = 120;
test3.c = 130;
//list1.push_back(test3);

//list1.sort();
put_list(list1,"list1");

///return 0;

INTLIST::reverse_iterator plist;
INTLIST::iterator plist1 = list1.end();
for(plist = list1.rbegin(); plist != list1.rend(); plist++)
{

printf("a = %d, b = %d n", ((Test*)&(*plist))->a, ((Test*)&(*plist))->b);
if(((Test*)&(*plist))->a <= test3.a)
{
break;
}

--plist1;

}

list1.insert(plist1, 1, test3);

//list1.insert(++list1.begin(),1,test3);
printf(" insert n");
put_list(list1,"list1");


return 0;
}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/2008/viewspace-1026135/,如需转载,请注明出处,否则将追究法律责任。

上一篇: list用法
下一篇: 资源管理器
请登录后发表评论 登录
全部评论

注册时间:2007-12-28

  • 博文量
    109
  • 访问量
    1260992