开发者

C++ equivalent of .ToString()

There must be an easy way to do this...

// C# code
    for (int i = 0; i < 20; i++)
        doSomething(i.ToString() + "_file.bmp");

I'm trying to do this in C++, but it turns out that the simplest things are the hardest to do in that language. Mostly because开发者_如何学JAVA there's a catch: I'm restricted to a library that only takes char*'s as the parameter for the function this will eventually end up in, so I'm pretty much stuck playing with char arrays. This is what I have so far:

char* path[12];
for(int i = 0; i < 20; i++)
{
    sprintf(path[0],"%i_Card.bmp",i);
    cards[i] = new Card(i,path[0]);
}

The problem is, this approach ends me up with one big, long, useless string.

I must disclose that this is for a school assignment, but answering this question will not decide my grade, it will just make one aspect of the app a little easier.


The C++03 equivalent of ToString is

std::stringstream stream;
stream << i;
std::string i_as_string = stream.str();

Note that you can also accomplish that with no intermediate variables, by doing ( std::stringstream() << i ).str().

In C++11 there is both std::lexical_cast< std::string >( i ) which does the above for you (also available from Boost), and std::to_string( i ).


Try this

#include <stdio.h>
#include <stdlib.h>

itoa(i)

atoi


Or you can go this route:

#include <sstream>

int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();


I see several bugs in your code.

1) You declare "path" to be an array of 12 character pointers, but no memory allocated for the any of the array items. The sprintf statement is guaranteed to copy into garbage memeory. I'm surprised this doesn't crash your program right away.

2) And even if there was memory allocated for the path array, your sprintf statement always copies to path[0] - overwriting what was already there.

I suspect you are confusing char arrays, strings, and arrays of strings in C/C++. Perhaps the code below will help. I'm assuming that your "Card" class doesn't save a copy of the string passed as the second parameter to a member variable (at least not without copying it). Otherwise, it will be pointing to stack memory - which could be buggy if your Card instance outlives the function in which it was created in.

const size_t MAX_INTEGER_LENGTH = sizeof(int) * 4; // 4x the sizeof int will suffice no matter what the sizeof(int) is

char szPostfix[] = "_Card.bmp"; 

for(int i = 0; i < 20; i++)
{
    char path[MAX_INTEGER_LENGTH + sizeof(szPostfix) + 1]; //+1 for null terminator
    sprintf(path,"%d%s",i, szPostfix);
    cards[i] = new Card(i,path);
}


You can use std::string and convert it to to the c-string using std::string::c_str(). However, it returns a c string of const char*. const_cast can be use to disqualify the const associated with it.

#include <iostream>
#include <string>
using namespace std;

void foo(char* str){
    cout << str << endl;
}

int main(){

    string str = "Hello World";
    foo(const_cast<char*>(str.c_str()));

    return 0;
}

Output: Hello World

Demo


There's no general way to convert an object to a string in C++. You have to define your conversion functions yourself. For basic types you can convert them to strings using stringstream.

#include <sstream>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
  stringstream ss;
  ss << 1;
  cout << ss.str();
}

There's more info about stringstream here


From the code it looks like it is a file name and the Card class should take the path as a const char*. So I think the following would be the c++ way of doing it.

for(int i = 0; i < 20; i++)
{
    std::stringstream out;
    out << i << "_Card.bmp";
    cards[i] = new Card(i,out.str().c_str());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜