- 论坛徽章:
- 0
|
这是写的一个简单测试程序:
下载地址:http://jinix.sourceforge.net/src/test_stlport.cpp
代码帖在下面:
- #include <vector>
- #include <list>
- #include <string>
- #include <algorithm>
- using namespace std;
- class my_struct {
- public:
- string name;
- int age;
- };
- class CAgeCompare
- {
- public:
- CAgeCompare(int age)
- {
- m_age = age;
- }
- bool operator ()(const my_struct *ms)
- {
- if (m_age == ms->age)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private:
- int m_age;
- };
- void do_test()
- {
- // test2
- typedef list<my_struct *> DataList;
- DataList list3;
- my_struct ms1; ms1.name = "name1"; ms1.age = 21; list3.push_back(&ms1);
- my_struct ms2; ms1.name = "name2"; ms1.age = 22; list3.push_back(&ms2);
- my_struct ms3; ms1.name = "name3"; ms1.age = 23; list3.push_back(&ms3);
- my_struct ms4; ms1.name = "name4"; ms1.age = 24; list3.push_back(&ms4);
- my_struct ms5; ms1.name = "name5"; ms1.age = 25; list3.push_back(&ms5);
- my_struct ms6; ms1.name = "name6"; ms1.age = 26; list3.push_back(&ms6);
- my_struct ms7; ms1.name = "name7"; ms1.age = 27; list3.push_back(&ms7);
- my_struct ms8; ms1.name = "name8"; ms1.age = 28; list3.push_back(&ms8);
- my_struct ms9; ms1.name = "name9"; ms1.age = 29; list3.push_back(&ms9);
- DataList::iterator it3 = find_if(list3.begin(), list3.end(), CAgeCompare(22));
- printf("age: %d\n", (*it3)->age);
- printf("name: %s\n", (*it3)->name.c_str());
- }
- int main(int argc, char *argv[])
- {
- do_test();
- }
复制代码 编译运行如下:
[wyt@localhost ~]$ g++ test_stlport.cpp && ./a.out
age: -1077360744
name: 见p
[wyt@localhost ~]$
[wyt@localhost ~]$
很显然不是我要的结果。是哪里出错了? |
|