can't use iteractor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>

using namespace std; 
int main()
{
    int a[]={1,2,3,4,100,5,100};
    vector<int> ivec(a,a+7);
    list<int>  list;
    typedef list<int>::iterator iter;
	iter it=list.begin();

    replace_copy(ivec.begin(),ivec.end(),inserter(list,list.begin()),100,0);
    
    cout <<"list:"<< endl;
    
    for(it;it!=list.end();++it)
	cout<< *iter <<"";
    
    cout << endl;
    
    return 0;


编译器: Default compiler
Building Makefile: "E:\C++\Work\Makefile.win"
执行  make clean
rm -f mm.o  工程1.exe

g++.exe -c mm.cpp -o mm.o -I"lib/gcc/mingw32/3.4.2/include"  -I"include/c++/3.4.2/backward"  -I"include/c++/3.4.2/mingw32"  -I"include/c++/3.4.2"  -I"include"   

mm.cpp: In function `int main()':
mm.cpp:12: error: expected init-declarator before '<' token

mm.cpp:12: error: expected `,' or `;' before '<' token
mm.cpp:13: error: `iter' undeclared (first use this function)
mm.cpp:13: error: (Each undeclared identifier is reported only once for each function it appears in.)
mm.cpp:13: error: expected `;' before "it"

mm.cpp:19: error: `it' undeclared (first use this function)

make.exe: *** [mm.o] Error 1

执行结束


who can tell me where I am error ??why??please!!
put typedef list<int>::iterator iter: line 12 above main method and convert line 20 to cout<< *it <<"";

Please learn using STL, namespace and iterators. Your code it's very horror.
Isn't that a dependant type? Try:
typedef typename list<int>::iterator iter;
I am very sorry , I am a beginners,so my code isn't standard ,I am learning to use STL

thxs evry one!!

correction follow as :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <stdlib.h> 
using namespace std; 

int main()
{   
	
    typedef list<int>::iterator iter;
    int a[]={1,2,3,4,100,5,100};
    vector<int> ivec(a,a+7);
    list<int>  list;
    replace_copy(ivec.begin(),ivec.end(),inserter(list,list.begin()),100,0);
    
    cout <<"list:"<< endl;
    for(iter it=list.begin();it!=list.end();it++)
		cout<< *it <<"  "; 
    cout << endl;
    system("pause");
    return 0;
} 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>

int main()
{
    int a[]={1,2,3,4,100,5,100};
    std::vector<int> ivec(a, a+sizeof(a) / sizeof(a[0]));
    typedef list<int>  Intlist;
    typedef Intlist::iterator iter;
    Intlist lst;

    replace_copy(ivec.begin(),ivec.end(), inserter(lst, lst.begin()),100,0);

    
    return 0;
}
Last edited on
why typedef list<int> ::iterator iter; isn't ? The book tells about it in a similar way
Topic archived. No new replies allowed.