Jump to content
  • Advertisement
Sign in to follow this  
Squirell

Vector/ Integer to Char* Questions

This topic is 4982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As shown by the title I have two questions: 1. How do you erase a random element in a vector (I know its slow but that doesn't matter for my program). For example say I want to erase the 3rd element in a vector of integers called MyVector. How would I do it? 2. I have an integer that I want to convert to a char*. How do I do this. ex. int x = 5; char* y = x; //Y now equals "5". Is there a function that does this. Thanks for the help.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
1. Vector::erase(iterator first, iterator last);
Make first and last the same thing

2. itoa for the cstdlib

Share this post


Link to post
Share on other sites
If you have a vector v, you can erase the nth element in v with v.erase(v.begin() + n).

itoa is not a standard function. To convert an integer into a string you can use boost::lexical_cast, a std::stringstream or snprintf(). With a std::stringstream you can just use operator<< to insert into the stringstream and use the str() member function.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
std::stringstream or snprintf(). With a std::stringstream you can just use operator<< to insert into the stringstream and use the str() member function.
to give you an idea of how each looks:

#include <sstream>

int ival = some_integer_value;
std::string output;
std::stringstream ss;
ss << ival;
ss >> output;
results: output now contains the string representation of ival

int ival = some_integer_value;
char buf[128];
_snprintf(buf, sizeof(buf), "%i", ival);

results: buff now contains the string representation of ival

PS. im not sure if snprintf is standard, std::stringstream is and itoa isn't.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!