请问下,find命令如何使用在LIST中。

sno_guo sno_guo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区


大家好,想练习下下面这个这个代码,发现在编译的时候总是出错,请问下为什么:
如果把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;
}
}
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
sno_guo
C++ 语言 65,214 成员 250,514 内容