const_iterator compiler error

Y

Yahooooooooo

Hi,

learning c++, can some one check below program.
its giving compiler error.

#include <iostream>
#include <list>
using namespace std;

typedef list<int> ListInt;

int main(void)
{
list<int> listInt;

for(int i=0;i<10;i++)
listInt.push_back(i*2);

list<int>::const_iterator cnt;
for(cnt=listInt.begin(); cnt!=listInt.end(); ++cnt);
cout << *cnt << " " <<endl;

return 0;
}



Regards,
M.Azmath
 
V

Victor Bazarov

Yahooooooooo said:
learning c++, can some one check below program.
its giving compiler error.

WHAT compiler error? The code is fine.
#include <iostream>
#include <list>
using namespace std;

typedef list<int> ListInt;

What's that for? You're not using it.
int main(void)
{
list<int> listInt;

for(int i=0;i<10;i++)
listInt.push_back(i*2);

list<int>::const_iterator cnt;
for(cnt=listInt.begin(); cnt!=listInt.end(); ++cnt);
cout << *cnt << " " <<endl;

return 0;
}

As I noted, the code is fine. You should consider posting
the compiler output as well...

V
 
A

Anant

What's the error are you getting? If this is VC++ then you will have to
include
#include "stdafx.h" header file else it will give you " fatal error
C1010: unexpected end of file while looking for precompiled header
directive".
 
S

Salt_Peter

Yahooooooooo said:
Hi,

learning c++, can some one check below program.
its giving compiler error.

#include <iostream>
#include <list>
using namespace std;

typedef list<int> ListInt;

int main(void)
{
list<int> listInt;

for(int i=0;i<10;i++)
listInt.push_back(i*2);

list<int>::const_iterator cnt;

A const_iterator is not involved here since the list itself is not
constant
for(cnt=listInt.begin(); cnt!=listInt.end(); ++cnt);
cout << *cnt << " " <<endl;

return 0;
}

Lets rectify that by passing the list by const reference to a function
so you'll see when a const_iterator is warranted. You'll want to become
familiar with operator<< as well.

#include <iostream>
#include <ostream>
#include <list>
#include <iterator> // for std::eek:stream_iterator< >

template< typename T >
void show(const std::list< T >& r_l)
{
// dependant type
typedef typename std::list< T >::const_iterator LIter;
for(LIter liter =r_l.begin();
liter != r_l.end();
++liter)
{
std::cout << *liter << std::endl;
}
}

template< typename T >
std::eek:stream&
operator<<(std::eek:stream& os, std::list< T >& r_l)
{
std::copy( r_l.begin(), // const_iterator returned
r_l.end(),
std::eek:stream_iterator< T >(os, "\n") );
return os;
}

int main()
{
std::list< int > nlist;

for(int i = 0 ; i < 10; ++i)
nlist.push_back( i * 2 );

show(nlist);

std::cout << "using op<< ..." << std::endl;
std::cout << nlist;
}

/*
0
2
4
6
8
10
12
14
16
18
using op<< ...
0
2
4
6
8
10
12
14
16
18
*/
 
S

Salt_Peter

Yahooooooooo said:
Hi,

learning c++, can some one check below program.
its giving compiler error.

#include <iostream>
#include <list>
using namespace std;

typedef list<int> ListInt;

int main(void)
{
list<int> listInt;

for(int i=0;i<10;i++)
listInt.push_back(i*2);

list<int>::const_iterator cnt;
for(cnt=listInt.begin(); cnt!=listInt.end(); ++cnt);
cout << *cnt << " " <<endl;

return 0;
}

The code above should compile fine although you don't require the
const_iterator.

Lets change that fact by passing the list by const reference to a
function
so you'll see when a const_iterator is warranted. You'll want to become
familiar with operator<< as well as algorithms like std::copy.

If you get an error, tell us what the error is.

#include <iostream>
#include <ostream>
#include <list>
#include <iterator> // for std::eek:stream_iterator< >

template< typename T >
void show(const std::list< T >& r_l)
{
// dependant type
typedef typename std::list< T >::const_iterator LIter;
for(LIter liter =r_l.begin();
liter != r_l.end();
++liter)
{
std::cout << *liter << std::endl;
}
}

template< typename T >
std::eek:stream&
operator<<(std::eek:stream& os, std::list< T >& r_l)
{
std::copy( r_l.begin(), // const_iterator returned
r_l.end(),
std::eek:stream_iterator< T >(os, "\n") );
return os;

}

int main()
{
std::list< int > nlist;

for(int i = 0 ; i < 10; ++i)
nlist.push_back( i * 2 );

show(nlist);

std::cout << "using op<< ..." << std::endl;
std::cout << nlist;
}
 
B

BobR

Victor Bazarov wrote in message ...
WHAT compiler error? The code is fine.


What's that for? You're not using it.

Let's comment this:

for( list<int>::const_iterator cnt( listInt.begin() );
cnt != listInt.end(); ++cnt ){
cout << *cnt << " " <<endl; // and dereference .end()
}
 
P

Pete Becker

Salt_Peter said:
A const_iterator is not involved here since the list itself is not
constant

There's nothing wrong with having an iterator that won't modify the
elements of a modifiable list. Just like having a const int* that points
to a modifiable int. In fact, standard containers as of C++0x have
member functions that give you back const iterators, so that you don't
have to cast to a const& to get one:

int i;
const int *ip = &i;

container c;
container::const_iterator it = ((const container&)c).begin();
container::const_iterator it = c.cbegin();

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
V

Victor Bazarov

Yahooooooooo said:
learning c++, can some one check below program.
its giving compiler error.

#include <iostream>
#include <list>
using namespace std;

typedef list<int> ListInt;

int main(void)
{
list<int> listInt;

for(int i=0;i<10;i++)
listInt.push_back(i*2);

list<int>::const_iterator cnt;
for(cnt=listInt.begin(); cnt!=listInt.end(); ++cnt);

OK. It is possible that it was intentional, but I only noticed it
after reading through other replies, and nobody else seems to have
noticed it. So... Did you mean to have a semicolon after the
closing parenthesis on the line above? If not, and you wanted to
simply output the entire list with spaces between elements, you
need to drop the semicolon.
cout << *cnt << " " <<endl;

Here 'cnt' equals 'listInt.end()'. Dereferencing it has undefined
behaviour, although I don't think it would produce a compiler error.
return 0;
}

V
 
S

Salt_Peter

Pete said:
There's nothing wrong with having an iterator that won't modify the
elements of a modifiable list. Just like having a const int* that points
to a modifiable int. In fact, standard containers as of C++0x have
member functions that give you back const iterators, so that you don't
have to cast to a const& to get one:

I'm aware of that, the above should have said "not required: instead of
"not involved".
As far as returning const_iterators, member functions already do that,
perhaps i'm missing something. I understand that C++0x will introduce a
keyword 'where' to access nested types like const_iterator. The auto
keyword will also allow type deduction for the iterator type in a for
loop.

for (auto p = v.begin(); p!=v.end(); ++p)
std::cout << *p << std::endl;
 
P

Pete Becker

Salt_Peter said:
As far as returning const_iterators, member functions already do that,

begin() and end() return modifiable iterators for containers that allow
modification of their elements.
perhaps i'm missing something. I understand that C++0x will introduce a
keyword 'where' to access nested types like const_iterator.

There was talk of using 'where' in concepts to describe requirements on
types. But 'where' is far too common a name, so it's going to be
something else. In any case, that isn't about access, but about
compile-time requirements.
The auto
keyword will also allow type deduction for the iterator type in a for
loop.

Yes, it will.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
D

Default User

Anant wrote:

What's the error are you getting? If this is VC++ then you will have
to include
#include "stdafx.h" header file else it will give you " fatal error
C1010: unexpected end of file while looking for precompiled header
directive".

This is nonsense. VC++ does not require such a thing.

Also, please don't top-post. Your replies belong following or
interspersed with properly trimmed quotes. See the majority of other
posts in the newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top