473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

const_iterator compiler error

Hi,

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

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

typedef list<intListInt;

int main(void)
{
list<intlistInt;

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

Dec 13 '06 #1
10 2405
Yahooooooooo wrote:
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<intListInt;
What's that for? You're not using it.
>
int main(void)
{
list<intlistInt;

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 13 '06 #2
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".

Yahooooooooo wrote:
Hi,

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

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

typedef list<intListInt;

int main(void)
{
list<intlistInt;

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
Dec 13 '06 #3

Yahooooooooo wrote:
Hi,

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

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

typedef list<intListInt;

int main(void)
{
list<intlistInt;

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::ostream_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::ostream&
operator<<(std::ostream& os, std::list< T >& r_l)
{
std::copy( r_l.begin(), // const_iterator returned
r_l.end(),
std::ostream_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
*/

Dec 13 '06 #4

Yahooooooooo wrote:
Hi,

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

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

typedef list<intListInt;

int main(void)
{
list<intlistInt;

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::ostream_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::ostream&
operator<<(std::ostream& os, std::list< T >& r_l)
{
std::copy( r_l.begin(), // const_iterator returned
r_l.end(),
std::ostream_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;
}

Dec 13 '06 #5

Victor Bazarov wrote in message ...
>Yahooooooooo wrote:
>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<intListInt;

What's that for? You're not using it.
>>
int main(void){
list<intlistInt;
Let's comment this:
> list<int>::const_iterator cnt; // declare iterator
for(cnt=listInt.begin(); cnt!=listInt.end(); ++cnt); // set it to
..end()
> cout << *cnt << " " <<endl; // and dereference .end()
for( list<int>::const_iterator cnt( listInt.begin() );
cnt != listInt.end(); ++cnt ){
cout << *cnt << " " <<endl; // and dereference .end()
}
>>
return 0;
}

As I noted, the code is fine. You should consider posting
the compiler output as well...
--
Bob R
POVrookie
Dec 13 '06 #6
Salt_Peter wrote:
>
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)
Dec 13 '06 #7
Yahooooooooo wrote:
learning c++, can some one check below program.
its giving compiler error.

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

typedef list<intListInt;

int main(void)
{
list<intlistInt;

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 13 '06 #8

Pete Becker wrote:
Salt_Peter wrote:

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:
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;
>
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)
Dec 13 '06 #9
Salt_Peter wrote:
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)
Dec 13 '06 #10
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

Dec 13 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: john smith | last post by:
Hi, I'm a little confused as to why the following code generates and error when compiling: #include <iostream> #include <vector> #include <iterator> using namespace std; void...
3
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the...
5
by: Joseph Turian | last post by:
In a class templatized by class T, I have the following two lines: hash_map<string, T> foo; hash_map<string, T>::const_iterator p; The first line compiles just fine. The second line gives the...
5
by: John Harrison | last post by:
I there a reliable and generic method to convert a const_iterator to an iterator (i.e. something like const_cast). I ask because I'm writing some methods which take and return iterators. A const...
1
by: flopbucket | last post by:
Hi, For the learning experience, I am building a replacement for std::map. I built a templated red-black tree, etc., and all the basic stuff is working well. I implemented basic iterators and...
3
by: Bit byte | last post by:
whats the difference - apart from what the name suggests? - i.e. one is const What are there pros and cons of using one over the other ?
2
by: ul | last post by:
Hello, Just wonder what is faster for std::vector, const_iterator od iterator? Or is this stl-realisation-dependent? Target compilator gcc platform is Linux. Thanks
4
by: kotau | last post by:
Hi, I'm having trouble with something that would appear to have a simple solution. Here's a version of the code I'm working with: const Item* p 0; name::const_iterator i;
2
by: Markus Dehmann | last post by:
I want to derive from std::set, like shown below. But when I try to declare an iterator over the contained elements I get an error, see the twp uncommented lines: #include <set> template<class...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.