Vectors

Thread Solved

Join Date: Nov 2009
Posts: 37
Reputation: gedas is an unknown quantity at this point 
Solved Threads: 0
gedas gedas is offline Offline
Light Poster

Vectors

 
0
  #1
Dec 7th, 2009
Hey everyone,
im trying to get to know vectors but i came to a point that my current knowledge stops and i need more help,
i found a program which i would like to understand but i couldn't figure it out.
here is the code:
  1. #include<iostream>
  2. #include<vector>
  3. #include<string>
  4. #include<iterator>
  5. using namespace std;
  6.  
  7. bool islowerCase(char c)
  8. {
  9. return islower(c);
  10. }
  11.  
  12. int main()
  13. {
  14. const int size=10;
  15. vector<char>v1;
  16. vector<char>::iterator lastElem;
  17.  
  18.  
  19. const char charList[size]={'w','W','b','B','C','E','w','F','B','m'};
  20.  
  21.  
  22. for(int i=0;i<size; i++)
  23. {
  24. v1.push_back(charList[i]);
  25.  
  26. }
  27. lastElem =remove_if(v1.begin(), v1.end(), islowerCase);//line1
  28. ostream_iterator<char> screen(cout, "");//line2
  29.  
  30.  
  31. copy(v1.begin(), lastElem, screen);//line3
  32. cout<<endl;
  33. }

can somebody explain to me line 1,2 and three (where it is shown as comments)
in detail because i am really struggling to understand how it works thanks a lot
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess
 
0
  #2
Dec 7th, 2009
Did you check your handy C++ library reference? Because it's all explained there, and a more specific question than "can you tell me everything I want to know?" is likely to encourage high quality answers.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,930
Reputation: firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all 
Solved Threads: 254
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Posting Virtuoso
 
1
  #3
Dec 7th, 2009
  1. lastElem =remove_if(v1.begin(), v1.end(), islowerCase);//line1

Line 1 : remove_if(...), if the containers contains a lower case character
the function removes it. It does this, and returns the a pointer that
points to the new end since some elements has been deleted.

  1. ostream_iterator<char> screen(cout, "");//line2
Line 2 : creates an iterator. Just like the vector::iterator, ostream_iterator is an iterator. It can insert elements into the
ostream object, cout. When you do :
  1. cout << 'a';
Its almost the same as if you do :
  1. screen = 'a';
Its just doing it in a different way.

  1. copy(v1.begin(), lastElem, screen);//line3

Remember what I said, if you do screen = 'a'; It will print out a into the
console. So the above code roughly does screen = vec[0] ... vec[lastElement]. That means everything inside the vector is getting
printed to the screen.


Also check this out for some reference : remove_if , istream_iterator using std::copy
Go ahead and try to solve  this  euler problem, if u can. Check with me for hints and 
answers. Good Luck
Solved by:tux4life, "your name here"
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 37
Reputation: gedas is an unknown quantity at this point 
Solved Threads: 0
gedas gedas is offline Offline
Light Poster
 
0
  #4
Jan 5th, 2010
thank you guys for your help thanks a lot
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 411 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC