Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL question

Reply
Thread Tools

STL question

 
 
Jerry Coffin
Guest
Posts: n/a
 
      05-29-2008
In article <a1bbefdf-903f-4fae-83a5-1272298e0393
@q27g2000prf.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...

[ ... ]

> // using vector<int>::iterator;


Here's (probably) the source of the problem: this wasn't originally
commented out.

> 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;


The original intent (with the previous line still "executable") was that
this refer to 'std::vector<int>::iterator'. With the previous line
commented out, it refers to 'std::iterator', which doesn't work.

> copied from the book <<Absolute C++>>, but I cannot compile, why?


I'd say "because you ended up with a lousy book. Even when/if you fix
the code so it compiles, I'd consider it a long ways from exemplary
code. If I was going to do what it does, I'd write it something like
this:

#include <iostream>
#include <vector>

void show(std::vector<int> const &container) {
std::copy(container.begin(), container.end(),
std:stream_iterator<int>(std::cout, " "));
std::cout << "\n";
}

int main()
{
std::vector<int> container;
for(int i=1;i<=4;i++)
container.push_back(i);

std::cout << "Here is what is in the container:\n";
show(container);

std::cout << "Setting entries to 0:\n";
std::fill_n(container.begin(), container.size(), 0);

std::cout <<"Container now contains:\n";
show(container);

return 0;
}

As a generalization, I'd say almost anytime you have something like:

some_type::iterator x;
for (x=container.begin(); x!=container.end(); ++x)
do_something();

You've _probably_ made a mistake. You should probably be using an
algorithm instead. In this case, he duplicated two algorithms already in
the standard library: std::fill_n and std::copy. Far better to use than
duplicate them. Another principle is often phrased as "don't repeat
yourself" -- in the previous code, he had two repetitions of identical
code to show the contents of the container. I've moved that into a
function of its own.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
 
foolsmart2005@gmail.com
Guest
Posts: n/a
 
      05-29-2008
On May 28, 10:34 pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> I use dev C++ to write my program,
> it occurs error, what does it mean, the program is ok running in VC++,
> but cannot in Dev C++.
>
> 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;
>
> 16 E:\Dev C++\stl001.cpp missing template arguments before "p"


The problem has been solved, thanks everyone.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation Fault on stl::resize / stl::clear Steve C++ 2 11-06-2007 06:53 AM
stl questions: how can I compare 2 stl list? silverburgh.meryl@gmail.com C++ 5 04-16-2006 09:57 PM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
To STL or not to STL Allan Bruce C++ 41 10-17-2003 08:21 PM



Advertisments