Hey what would be the correct syntax for a vector of char* with a size of [2]? ive got it written like this..

vector< char* > myVector();
vector< char* > ::iterator myIterator;

// reads in something

myVector.push_back(temp);

myIterator = myVector.begin();
cout << myIterator << endl;

why does it cout nothing?

Container iterator is an analog of a pointer, you must derefence it to get a value:

cout << *myIterator << endl;

It's interesting, what's your temp variable?..

Creating a vector of char* is a dangerous thing to do, as if a pointer to a string was added to the vector in a different scope, the data that the pointer is pointing to may not be valid anymore. For example, the following will work:

vector< char* > myVector;
vector< char* > ::iterator myIterator;

char temp[2] = "t";
myVector.push_back( temp );

myIterator = myVector.begin();
cout << *myIterator << endl;

However, this may not:

vector< char* > myVector;
vector< char* > ::iterator myIterator;

// Some condition
if ( true ) {

  char temp[2] = "t";
  myVector.push_back( temp );

}

myIterator = myVector.begin();
cout << *myIterator << endl;

To be safe, it's easiest to simply create a small structure and remove that risk altogether.

struct mystruct {
  char temp[2];
};

int main() {
  vector<mystruct> myVector;
  vector<mystruct> ::iterator myIterator;

  mystruct temp = {"t"};
  myVector.push_back( temp );

  myIterator = myVector.begin();
  cout << (*myIterator).temp << endl;
}

Hope this helps.

Comments
good post
Good post !
Nice post

wow thank you so much, that was really helpful :) if its not safe to use char* how would i change a char* to a char?

why not just use a string?

int main()
{
    std::vector <std::string> test;
    
    test.push_back("aa");
    test.push_back("aa");
    test.push_back("aa");
    test.push_back("aa");

   int len = test.size();
    for ( int i = 0; i < len; i++ )
    {
      std::cout << test[i] << std::endl;
    }
    std::cin.get();
}

Serkan, what a f****** idiot are you?? :angry:
You just can't have a single reason to give WH bad rep for such a useful post!
And don't say that it was by accident, you can only give good rep by accident, not bad rep!

Sorry for my behaviour :$ ...

Grrroooaar

Relax man, it's only reputation. Besides, we all know who gave it to William, so my advice is not to take it too seriously :)

Comments
Hehe :P

It didn't really annoy me at all, especially when you realize that originally that post boosted my rep by 42 points, and thanks to serkan that dropped to 40... :(

Thanks tux, but remember not to take it too seriously, especially when it's not your own rep.

Comments
*nods*
This article has been dead for over six months. Start a new discussion instead.