Jump to content

Vector to String and Reverse

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Kyle Joseph Klouzal

Kyle Joseph Klouzal

    Newbie

  • Members
  • PipPip
  • 13 posts
I'm not quite sure how I can convert my vectors to strings to store them in my database, then take the strings from the database and format them back into vectors.

vector<string> FriendsList;

FriendsList.push_back("user1");

FriendsList.push_back("user2");

FriendsList.push_back("user3");

FriendsList.push_back("user4");

FriendsList.push_back("user5");


If I looped through that i could print the values into a string and put commas after each one, then do something like that in reverse to look for the commas to separate the values.
But I thought there was a function for that already?
I don't need to use vectors, anything that might have a function for this would work.

Thanks! :D

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You could use an iterator in a loop.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Kyle Joseph Klouzal

Kyle Joseph Klouzal

    Newbie

  • Members
  • PipPip
  • 13 posts
Ok thanks, I like others opinions on how to do things, I'm trying to make a friends list for my game, here the server saves the vector values as a string into the mysql database, loads them back as a string then converts back to a vector.
I was already thinking loop through and check for comma but thought there might be some easier way out there so I asked :P

Now that I'm thinking about it though, if someone has quite the large friends list, when my server sends it to them the packet size may exceed the maximum and it not go through..
I suppose there I could loop it to only send 10 or so amount at a time until it's done..




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users