This is a easy question about Standard Template Library(STL). I have used "vector" class of type "char *" in my program. I have appended below my complete program (run in g++ compliler). Here v1 is a vector of type "char *". I have pushed (using the method push_back) in v1 a variable "vtop" (of type "char *"). Initially the variable "vtop" contains "EECS171". Now, when I print th econtent of the vector v1, I get that its only content is "EECS171". That is alright...
But now I assign (after I pushed_back to the vector v1) a new string of char * to the variable vtop. Say the new char string is "EECS370". Now when I print the content of the vector v1, surprisingly I find that its only content is "EECS370" (NOT "EECS171" !!!). I wonder who the vector v1 gets changed when I only modified the content of the variable "vtop"...
Here is the program...
If you have any idea of why this is happening, pls let me know....
Thanks a lot...
My complete program:
using namespace std;
void printVector(const vector<char *> &vec);
vector<char *> v1; //Stores POSTFIX string
vtop = new char ;
strcpy (vtop, "EECS171");
strcpy(vtop, "EECS370"); //This above line surprisingly changes the vector v1 !!!!!
cout<<"v1 is: ";
void printVector(const vector<char *> &vec)
vector<char *>::const_iterator p1;
for (p1=vec.begin(); p1 != vec.end(); ++p1)