Hello everyone!
I am programming a vector which stores a name and a number.

typedef pair<int,string> Users_Pair;
vector<Users_Pair> Users_Name;

And the vector expands and contracts through out the life of the program. But when I use erase all elements above the one being erased loose their string? I am guessing its to do with the memory address or pointer? I thought char* where the ones which needed that?...

Any help or guidance would be much appreciated!
Thank you in advance!
will,

Erase will call the string's destructor hence destroying the string object.

Well you really are going to have to give us a little more code. There are no obvious things that you are doing wrong:

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>

int
main()
{
  std::ostringstream cx;
  typedef std::pair<int,std::string> PT;
  std::vector<PT> XX;
  for(int i=0;i<10;i++)
    {
      cx<<"a";
      XX.push_back(PT(i,cx.str()));
    }
  // Erase some stuff:
  XX.erase(XX.begin()+3,XX.begin()+5);
  // Write it out:
  for(unsigned int i=0;i<XX.size();i++)
    std::cout<<"XX == "<<XX[i].first<<" "<<XX[i].second<<std::endl;
}

The above code deletes a section from the vector and shows that the strings haven't been deleted. If that works on your system, you are going to have to enrich that program until it has your error.

Have you checked that you are not miss using an iterator e.g.

std::vector<PT>::iterator Iter;
for(Iter=XX.begin();Iter!=XX.end();Iter++)
  {
     if (Iter->first==3)
       XX.erase(Iter);   // THIS INVALIDATES Iter.
  }

Beyond that you will have to post a bit more code. sorry couldn't be more help.

Thanks for the prompt reply, Here are more snippets of my code:

Users_Name.push_back(Users_Pair(Server_PlayerJoined,""));
for(int i = 0; i < Users_Name.size();i ++)
	{
		if(Users_Name[i].first == Server_PlayerLeft)
		{
			char Message[35];
			strcpy(Message,const_cast< char* > ( Users_Name[i].second.c_str()));
			strcat(Message,": Logged out.");
			NetPutByte(Data_Server_SendNewMessage);
			NetPutString(Message);
			NetSendAll();
			Users_Name.erase(Users_Name.begin() + i);
			break;
		}
	}

These are the beginning of the strings life and the end.. there really isn't that much more to it. Speak soon!
Will,

Can you post a complete program that exhibits the problem? Troubleshooting through snippets is generally unproductive.

Thank you for your generally unproductive post ;) I fixed it, and you could of fixed it from the snippets shown... It was to do with

const_cast< char* >

As it was to be moved around in the vector, it became vacant.

Thank you anyways!
From Will,

>Thank you for your generally unproductive post
Thank you for wasting everyone's time by not asking a smart question. I'm glad you solved your problem though.

Ummm, I think it was quite a valid question to be honest. ..? CLOSED.

It was a perfectly valid question, but you failed to ask it in such a way as to help others answer to your satisfaction.

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.