Vector/ Integer to Char* Questions
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.
1. Vector::erase(iterator first, iterator last);
Make first and last the same thing
2. itoa for the cstdlib
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.
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 SiCraneto give you an idea of how each looks:
std::stringstream or snprintf(). With a std::stringstream you can just use operator<< to insert into the stringstream and use the str() member function.
#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 ivalint 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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement