| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi,
is there a way to include a integer variable in a string? Example: string A; int N; A = "image N" //where N will be replaced with what is in the integer variable N. //like when we do printf("inage %d", N) but storing the string in the variable without printing. Thanks |
|
#2
|
|||
|
|||
|
Re: is there a way to include a integer variable in a string?
sprintf
|
|
#3
|
|||
|
|||
|
Re: is there a way to include a integer variable in a string?
Thankyou!
|
|
#4
|
|||
|
|||
|
Re: is there a way to include a integer variable in a string?
It is undefined to use sprintf on a std::string ... use stringstream
Example: Code:
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
template <typename T1 , typename T2>
T2 Convert(const T1 & t1)
{
T2 t2 = T2();
std::stringstream ss; // include <sstream>
ss << t1;
ss >> t2;
return t2;
}
int main(int argc , char* argv[])
{
int n = 10;
string s = "image ";
s += Convert<int,string>(n);
cout << s << "\n";
return 0;
}
|
|
#5
|
|||
|
|||
|
Re: is there a way to include a integer variable in a string?
You're right, I should have read the post more carefully. I thought he was asking about a char array.
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|