Sign in to follow this  

Vector/ Integer to Char* Questions

This topic is 4767 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
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

This topic is 4767 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this