Vector/ Integer to Char* Questions

Started by
3 comments, last by SiCrane 19 years, 4 months ago
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.
Advertisement
1. Vector::erase(iterator first, iterator last);
Make first and last the same thing

2. itoa for the cstdlib
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.
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.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
snprintf() was standardized in C99.

This topic is closed to new replies.

Advertisement