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:

#include<iostream>
#include<vector>
#include<string>
#include<iterator>
using namespace std;

bool islowerCase(char c)
{
  return islower(c);
}

int main()
{
  const int size=10;
  vector<char>v1;
  vector<char>::iterator lastElem;
 

  const char charList[size]={'w','W','b','B','C','E','w','F','B','m'};
 

  for(int i=0;i<size; i++)
    {
      v1.push_back(charList[i]);

    }
  lastElem =remove_if(v1.begin(), v1.end(), islowerCase);//line1
  ostream_iterator<char> screen(cout, "");//line2
 

  copy(v1.begin(), lastElem, screen);//line3
  cout<<endl;
}

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

Recommended Answers

All 3 Replies

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.

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.

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 :

cout << 'a';

Its almost the same as if you do :

screen = 'a';

Its just doing it in a different way.

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

thank you guys for your help thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.