请问下,find命令如何使用在LIST中。
大家好,想练习下下面这个这个代码,发现在编译的时候总是出错,请问下为什么:
如果把list<T> 中的T 换成int或string都可以,但自己定义的结构体或类,就不行,请问下为什么,谢谢了
#include <list>
#include <algorithm>
#include <iostream>
typedef struct __numTest
{
int v1;
int v2;
}numTest;
int main( )
{
using namespace std;
numTest n1;
numTest n2;
numTest n3;
numTest n4;
n1.v1=0;
n1.v2=1;
n2.v1=10;
n2.v2=11;
n3.v1=14;
n3.v2=16;
n4.v1=20;
n4.v2=24;
list <numTest> L;
list <numTest>::iterator Iter;
list <numTest>::iterator result;
numTest test;
L.push_back( n1 );
L.push_back( n2 );
L.push_back( n3 );
L.push_back( n4 );
cout << "L = ( " ;
for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
{
test=*Iter;
cout << test.v1 << " ";
}
cout << ")" << endl;
result = find( L.begin(), L.end(), n4); //总是在这里出错,请问下为什么,谢谢
if ( result == L.end() )
cout << "There is not in list L." << endl;
else
{
// result++;
test=*result;
cout << "There is in list L and it is"
<< " v1 : " << test.v1 << "." << endl;
}
}