This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
How do you change an integer to a string in c++?
Standard C++ library style:
#include <sstream>
#include <string>
(...)
int number = 5;
std::stringstream ss;
ss << number;
std::string numberAsString(ss.str());
Or if you're lucky enough to be using C++11:
#include <string>
(...)
int number = 5;
std::string numberAsString = std::to_string(number);
You could use snprintf(char *str, size_t size, const char *format, ...) to get a char[], then use string(char*) get string.
Of courseļ¼there're other ways.
Related
This question already has answers here:
String plus Char - what is happening?
(5 answers)
Closed 8 months ago.
#include <iostream>
#include <conio.h>
#include <stack>
#include <string>
using namespace std;
int main{
string h = "";
h = ("" + 'a');
cout << h;
return 0;
}
Output: "nity\VC\Tools\MSVC\14.29.30133\include\xstring"
I am honestly clueless as to what to do. I've never had this happen before.
Note: I've found a way to avoid this by appending the char like this:
string g="";
g+='a';
Regardless, why is this?
"" is a literal of type const char[1], which is the identical as const char* in most regards. 'a' is a literal of type char, which is really just an integer type. So if you do "" + 'a', you will get a pointer to 'a' (=97 in ASCII) characters after wherever the compiler decides to put the "". Which is then converted to an std::string.
In the working example, you convert the "" literal to std::string first, then add a char to it. std::string overloads the + and += operators, so it will produce a reasonable result.
This question already has answers here:
C++, copy set to vector
(8 answers)
How to copy std::string into std::vector<char>? [duplicate]
(2 answers)
Closed 2 years ago.
The following code prints an empty string and i am unable to understand why it is so?
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string s="hello";
std::string r;
std::copy(s.rbegin(),s.rend(), r.begin());
std::cout<<r;
return 0;
}
The problem is r is an empty std::string, it contains no chars. std::copy is trying to copy-assign chars since r.begin(), which leads to UB.
You can make r containing 5 elements in advance.
std::string r(5, '\0');
Or
std::string r;
r.resize(5);
Or use std::back_inserter.
std::string r;
std::copy(s.rbegin(),s.rend(), std::back_inserter(r));
This question already has answers here:
How to concatenate a std::string and an int
(25 answers)
Closed 4 years ago.
I've a function in a library (so, that I cannot change) like this:
char mName[MAX_PARAM_NAME_LEN];
void IParam::InitBool(const char* name) {
strcpy(mName, name);
}
I'd like to pass text as Text0, Text1 (and so on) "faster", writing directly inside the function, starting from a text and an integer, without store additional variables on my own; such as:
int mIndex = 0;
InitBool("Text" + mIndex);
How would you do it? Wrap functions? Which one? Best approch? In C# thats pretty done, I find hard to do it in C++.
If your compiler supports C++17 features you could use a fold expression and string stream. The magic happens in the stringify() function which accepts zero or more arguments.
#include <iostream>
#include <sstream>
#include <string>
template <typename... Ts>
std::string stringify(const Ts&... args)
{
std::ostringstream oss;
(oss << ... << args);
return oss.str();
}
void InitBool(const char *name)
{
std::cout << name << '\n';
}
int main()
{
int mIndex = 0;
InitBool(stringify("Text", mIndex, '!', 1.0/3.0).c_str());
}
Live Demo
In C++ "Text" is a const char[N], it's not actually a string type but just an array of characters with a null character ('\0') at the end. This doesn't support any sort of string manipulation. What you need to get is a std::string, which does support many string operations. Since you need to convert mIndex to a string to begin with we can just to that and the string that represents the number will handle concatenating "Text" to it. That gives you
int mIndex = 0;
InitBool(("Text" + std::to_string(mIndex)).c_str());
The ("Text" + std::to_string(mIndex)) part gives you a temporary std::string that is "Text0" and then the .c_str() gets a const char* to that string to pass to the function.
You can wrap the ("Text" + std::to_string(mIndex)) part in a function like
std::string concat(const char* str, int val)
{
return str + std::to_string(val);
}
and then the function call would look like
InitBool(concat("Text", mIndex).c_str());
This question already has answers here:
How to convert a number to string and vice versa in C++
(5 answers)
Closed 9 years ago.
#include <iostream>
using namespace std;
int main() {
string a = "1234"; //How this string convert in integer number
system("pause");
return EXIT_SUCCESS;
}
string a = "1234";
How this convert in integer
You can use std::stoi() to convert a std::string to an int.
#include <iostream>
#include <string>
int main() {
std::string a = "1234"; //How this string convert in integer number
int b = std::stoi(a);
system("pause");
return EXIT_SUCCESS;
}
If you have C++11 and onwards, use
int n = std::stoi(a);
(Pre C++11, you could use std::strtol;)
You could use boosts lexical cast
#include <boost/lexical_cast.hpp>
std::string str_num = "12345";
int value = 0;
try
{
value = boost::lexical_cast<int>(str_num);
}
catch(boost::bad_lexical_cast &)
{
// error with conversion - calling code will deal with
}
This way you can easily modify the code to deal with float or double if your string contains those types of numeric value also
You have to use std::stoi:
#include <iostream>
#include <string>
std::string s = "123";
int number= std::stoi(s);
The C++ Standard has a special function
int stoi(const string& str, size_t *idx = 0, int base = 10);
Probably you can try this
string a = "28787" ;
int myNumber;
istringstream ( a) >> myNumber;
See or you can search for stoi function and see how it can be used. Probably It can work but never try because I dont have the compiler of c++
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert a number to string and vice versa in C++
how to convert from int to char*?
I am getting a user input of integers and I need to pass them to an argument - Output(char const* str); This is a Class constructor. Can you please tell me how do I do this? Thank you
In C++11:
dodgy_function(std::to_string(value).c_str());
In older language versions:
std::ostringstream ss;
ss << value;
dodgy_function(ss.str().c_str());
// or
dodgy_function(boost::lexical_cast<std::string>(value).c_str());
// or in special circumstances
char buffer[i_hope_this_is_big_enough];
if (std::snprintf(buffer, sizeof buffer, "%d", value) < sizeof buffer) {
dodgy_function(buffer);
} else {
// The buffer was too small - deal with it
}