I do not know what to do to solve this problem with iterators

Write your question here.
I typed this program as it is in the textbook by Walter Savitch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  Put the code you need help with here.
//Program to demonstrate STL iterators
#include<iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;
using std::vector<int>::iterator;

int main() {
	
	vector<int> container;
		
	for (int i = 1; i <= 4; i++) 
		container.push_back(i);
		
	cout << "Here is what is in the container:\n";
	iterator p;
	for (p = container.begin()); p != container.end(); p++)
		cout << *p << " ";
	cout << endl;
	
	cout << "Setting entries to 0:\n";
		*p = 0;
		
	cout << "Container now contains:\n";
	for (p = container.begin(); p != container.end(); p++)
		cout << *p << " ";
	cout << endl;
	
	return 0;
}

When I compile I get this error:
8 25 C:\Dev-Cpp\SavitchChapter18\IteratorVector.cpp [Error] 'std::vector<int>' is not a namespace
C:\Dev-Cpp\SavitchChapter18\IteratorVector.cpp In function 'int main()':
18 2 C:\Dev-Cpp\SavitchChapter18\IteratorVector.cpp [Error] 'iterator' was not declared in this scope
18 2 C:\Dev-Cpp\SavitchChapter18\IteratorVector.cpp [Note] suggested alternatives:
65 0 c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h In file included from c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h
39 c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\char_traits.h from c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\char_traits.h
40 c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\ios from c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\ios
38 c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\ostream from c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\ostream
39 c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\iostream from c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\iostream
2 C:\Dev-Cpp\SavitchChapter18\IteratorVector.cpp from C:\Dev-Cpp\SavitchChapter18\IteratorVector.cpp
118 12 c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\stl_iterator_base_types.h [Note] 'std::iterator'
118 12 c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\stl_iterator_base_types.h [Note] 'std::iterator'
18 11 C:\Dev-Cpp\SavitchChapter18\IteratorVector.cpp [Error] expected ';' before 'p'
19 7 C:\Dev-Cpp\SavitchChapter18\IteratorVector.cpp [Error] 'p' was not declared in this scope
change using std::vector<int>::iterator; to std::iterator
change iterator p to vector<int>::iterator p; // because you need to declare iterator to a vector
You need to mention what alias you want to use for std::vector<int>::iterator. If you want it to be called iterator you need to write as follows.

 
using iterator = std::vector<int>::iterator;
We are getting there.
The program is now like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Program to demonstrate STL iterators
#include<iostream>
#include <vector>
#include<iterator>

using std::cout;
using std::endl;
using std::vector;
using std::iterator;

int main() {

	
	vector<int> container;

	for (int i = 1; i <= 4; i++) 
		container.push_back(i);
		
	cout << endl << "Here is what is in the container:\n" << endl;

	vector<int>::iterator p; // because you need to declare iterator to a vector
	
	for (p = container.begin(); p != container.end(); p++)
		
		cout << *p << " ";
	cout << endl;
	
	cout << endl << "Setting entries to 0:\n";
		*p = 0;
		
	cout << endl << "Container now contains:\n" << endl;
	for (p = container.begin(); p != container.end(); p++)
		
		cout <<  *p << " ";
	cout << endl;
	
	return 0;
}

Now here is the output:

Here is what is in the container:

1 2 3 4

Setting entries to 0:

Container now contains:

1 2 3 4

--------------------------------
Process exited after 7.756 seconds with return value 255
Press any key to continue . . .

It does not set the entries to 0 0 0 0
as it is in the textbook
Check your book example again. You probably have a loop construct missing between these two lines.
1
2
	cout << "Setting entries to 0:\n";
		*p = 0;
Thank you Peter87. It worked fine.
But there is something wrong with the book of Walter Savitch. Maybe too old;
Copyright 2006 by Pearson Education Inc
I don't have the book so I don't know what's wrong with it but 2006 is quite old. There was a big update to C++ in 2011 and a smaller one in 2014.

https://en.wikipedia.org/wiki/C++11
https://en.wikipedia.org/wiki/C++14
Last edited on
Topic archived. No new replies allowed.