ubiquibacon
ubiquibacon

Reputation: 10677

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);
} 

Upvotes: 7

Views: 4076

Answers (8)

missingfaktor
missingfaktor

Reputation: 92096

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);
}

Upvotes: 7

Sergey Teplyakov
Sergey Teplyakov

Reputation: 11657

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;
}

Upvotes: 3

anon
anon

Reputation:

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"; 
     }
}

Upvotes: 0

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() );

Upvotes: 1

Terje Mikal
Terje Mikal

Reputation: 947

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

Upvotes: 0

Ian G
Ian G

Reputation: 10939

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);

Upvotes: 0

Michael Howard-MSFT
Michael Howard-MSFT

Reputation: 3290

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!

Upvotes: 0

reko_t
reko_t

Reputation: 56450

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;
}

Upvotes: 12

Related Questions