tags:

views:

529

answers:

8
+7  Q: 

C++ string how to

This is a very simple question and I feel stupid for asking it, but I am pressed for time and I need to figure it out :)

I just need to know how to make a string that contains text and other variables. For instance in Java I can just do this:

String someString;

for(int i = 0; i>10; i++){

someString = ("this text has printed " + i + " times"); //how do I create this line in C++?

System.out.println(someString);

i++;

}

EDIT 4:

Ok, Rahul G's answer below works pretty good, and the program compiles and ok, but when I run it instead of getting the string I want for the file name, I get a bunch of numbers. For instance: << "frame " << i << " .jpg" creates: "013679000.jpg" instead of "frame 0.jpg" like I want. Any thoughts?

for(int i = 0; frames; i++)
{  
  frame = cvQueryFrame(capture); 
  std::string s = static_cast<std::ostringstream &>(std::ostringstream() << argv[1] <<  i << " .jpg").str(); 
  cvSaveImage(s.c_str(), frame);
} 
+12  A: 

You can use stringstreams for this:

for (int i = 0; i < 10; i++) {
    std::ostringstream ss;
    ss << "this text has printed " << i << " times";
    std::cout << ss.str() << std::endl;
}
reko_t
@reko_t's answer is probably better than mine as his code uses C++ constructs, mine is using a C runtime function. Both are valid for your example, however.
Michael Howard-MSFT
For a beginner question it would be helpful to show the required includes: `<sstream>`, `<ostream>` and `<iostream>`.
Charles Bailey
yes charles, thanks for pointing those includes out, I know diddly about c++
typoknig
A: 

You know you have a bug in your code?? You're incrementing i in the for() statement and at the end of the loop!! But I digress.

Use printf:

printf("This text has printed %d times\n",i)

Hope that helps!

Michael Howard-MSFT
+8  A: 

Java:

int i = 5;
double d = 2.23606798;
String s = "Square root of "+i+" is "+d;

C++:

int i = 5;
double d = 2.23606798;
std::ostringstream oss;
oss << "Square root of " << i << " is " << d;
std::string s = oss.str();
// If you need C style string...
char const *s0 = s.c_str();

Please note that the std::ostringstream class resides in <sstream> header.

Edit:

Your code (corrected):

for(int i = 0; frames; i++) { 
  frame = cvQueryFrame(capture);
  std::ostringstream oss;
  oss << "frame " << i << " .jpg";
  cvSaveImage(oss.str().c_str(), frame);
}
missingfaktor
This won't work, as the result of your bracketed expression containing the stringstream is an ostream, which has no str() member.
anon
@Neil: Thanks for pointing that out. Added an appropriate cast to make that work.
missingfaktor
@Rahul I don't see why you feel the need to create the stringstream as a nameless temporary. Giving it a name is actually clearer, shorter, and given the OP's C++ skills, easier for a newbie to understand.
anon
SUCCESS! Thank you all very much! I think all of you know what is going on, OpenCV just sucks, I suggest avoiding it unless you have no other choice :)
typoknig
@Neil: I wanted to make it look more like the Java version. Anyways, I edited the code again to make it easy to understand for newbies. Thanks for the suggestions.
missingfaktor
Ok, the program compiles and ok, but when I run it instead of getting the string I want for the file name, I get a bunch of numbers. For instance:<< "frame " << i << " .jpg"creates:"013679000.jpg" instead of "frame 0.jpg" like I want. Any thoughts?
typoknig
@typoknig: Try the new code. The reason why that didn't work is described here: http://stackoverflow.com/questions/2433071/turning-temporary-stringstream-to-c-str-in-single-statement/2433143#2433143
missingfaktor
It doesn't like the new code. I get the following 3 errors: error C2065: 's' : undeclared identifier error C2228: left of '.str' must have class/struct/union error C2228: left of '.c_str' must have class/struct/union It likes your other code though.
typoknig
My bad. It should be `oss`, not `s`. **Again** edited my post... <sigh>
missingfaktor
@Rahul I really suggest you compile your code before posting!
anon
@Neil: will do that from next time :-)
missingfaktor
Haha, you are doing much better than I would do, plus it is late! You were right though, that got it fixed. Tomorrow when I am a little more lucid I will look over those other threads and see why this works like it does. Writting in c++ is tough when a barely know .net or java :)
typoknig
A: 

If the error you're getting is "error C2664: 'cvSaveImage' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const char *" then you need a C style string not a C++ one:

cvSaveImage(oss.str().c_str(), frame);
Ian G
A: 

How about cvSaveImage(oss.str().c_str(), frame); ? That will make an old C-type zero terminated string which hopefully OpenCV will accept.

Terje Mikal
+1  A: 

I posted some code to create strings in place here (basically a wrapper to hide the std::ostringstream and make calling code cleaner). The usage would be:

void f( std::string const & ); // or std::string, but not std::string&

int var = 5;
f( make_string() << "prefix " << var << " postfix" );

Since you need a const char * you should use:

void g( const char * );
std::string s = make_string() << "prefix " << var << " postfix";
g( s.str() );
David Rodríguez - dribeas
+1, cool trick :-)
missingfaktor
A: 

This is a version of your latest code with your specific function cals removed so I can compile it. It compiles and works:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {

    for(int i = 0; i < 10; i++) { 
        ostringstream os;
        os << "frame" << i << " .jpg";
        string s = os.str();
        cout << s << "\n"; 
     }
}
anon
+3  A: 

There is one more way to do this: use boost::lexical_cast (I know, that it based on std::stringstream, but it pretty useful):

#include <string>
#include <iostream>
#include <boost\lexical_cast.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
    int i = 0;
    std::string result = "this text has printed " + boost::lexical_cast<std::string, int>(i) + " times";
    std::cout<<result<<std::endl;
    std::cin.get();
    return 0;
}
Sergey Teplyakov
This approach should be encouraged.Boost::lexical_cast<> is concise, self-documenting and in keeping with other C++ cast or conversion operations. I believe lexical_cast has been proposed for C++ TR2, so it may become standard (although not soon).
Rhubbarb