GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Feb-2005, 18:14
twigboy twigboy is offline
New Member
 
Join Date: Feb 2005
Posts: 7
twigboy is on a distinguished road

using vector push_back with iterator


hi all
i want to be able scan text moving from word to word with an iterator and which ever that word the iterator is on push it into a vector

this is what i have but i get an error
CPP / C++ / C Code:
string format_paragraph(vector<string> words, int text_width)

vector<string>::iterator pos;
for(pos=words.end();pos != words.begin(); pos++)
words.push_back(pos);
Last edited by LuciWiz : 07-Feb-2005 at 00:09. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 05-Feb-2005, 07:03
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
Find a good STL reference, perhaps SGI's STL guide or this site. If you prefer a hard copy, I recommend getting your hands on "The ++ Standard Library," by Josuttis. It is important to understand the interface you plan to use before trying to use it in your own code.

If you are using g++, the error message you saw probably looked a lot like this.

Quote:
myprogram.cpp: In function `std::string format_paragraph(std::vector<std::string, std::allocator<std::string> >, int)':
myprogram.cpp:9: error: no matching function for call to `std::vector<std::string, std::allocator<std::string> >:: push_back(__gnu_cxx::__normal_iterator<std::string *, std::vector<std::string,std::allocator<std::string > > >&)'
/usr/include/c++/3.3/bits/stl_vector.h:596: error: candidates are: void std::vector<_Tp, _Alloc>:: push_back(const _Tp&) [with _Tp = std::string, _Alloc = std::allocator<std::string>]

It looks complicated, but learning to read and understand these compiler messages is extremely important when programming. This message says that you are passing the wrong type of argument to the method push_back.

The push_back method on container vector takes an argument of type vector<_Tp>::value_type const&. Put simply, if you have a vector of strings, the method takes a string as its single argument, not an iterator that points to a string in your vector.

The syntax for iterators is much like that for pointers. An iterator can be dereferenced to get the value, or object that is pointed to, like so:
CPP / C++ / C Code:
words.push_back(*pos);

Now, sorry to defer this to the end, but you have got two other problems here.

First, you are starting position at the end of the vector, but you are using operator++ to increment it.
CPP / C++ / C Code:
for(pos=words.end();pos != words.begin(); pos++)
You will never get to the beginning of the vector by starting at the end an moving forward.

Second, you are modifying the vector with push_back while using the iterator, which essentially makes that iterator useless, or at least, unpredictable.

Think about what it is that you are really trying to accomplish here. I'll be happy to help by answering specific questions if you have them.

Matthew
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:33.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.