C ++ string like

This is a very simple question, and I feel stupid asking about it, but I click on the time and I need to find out :)

I just need to know how to create a string containing text and other variables. For example, 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, the Rahul G answer below works very well, and the program compiles and works fine, but when I run it instead of getting the line I want for the file name, I get a bunch of numbers. For example: << "frame " << i << " .jpg" creates: "013679000.jpg" instead of "frame 0.jpg" as 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); } 
+7
c ++ string
source share
8 answers

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

Note that the std::ostringstream is in the <sstream> header.

Edit:

Your code (fixed):

 for(int i = 0; frames; i++) { frame = cvQueryFrame(capture); std::ostringstream oss; oss << "frame " << i << " .jpg"; cvSaveImage(oss.str().c_str(), frame); } 
+7
source share

You can use string streams 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; } 
+12
source share

There is another way to do this: use boost :: lexical_cast (I know it is based on std :: stringstream, but it is very 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; } 
+3
source share

I put the code for creating the strings in place here (basically this is a wrapper to hide std::ostringstream and make a code cleaner). The following is used:

 void f( std::string const & ); // or std::string, but not std::string& int var = 5; f( make_string() << "prefix " << var << " postfix" ); 

Since you need const char * , you should use:

 void g( const char * ); std::string s = make_string() << "prefix " << var << " postfix"; g( s.str() ); 
+1
source share

Do you know that you have a bug in the code? You increase me in the for () expression and at the end of the loop! But I'm distracted.

Use printf:

printf ("% d times \ n" was printed in this text, i)

Hope this helps!

0
source share

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

 cvSaveImage(oss.str().c_str(), frame); 
0
source share

How about cvSaveImage(oss.str().c_str(), frame); ? This will create an old line with a null ending line, which we hopefully accepts OpenCV.

0
source share

This is the version of your latest code with a specific function 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"; } } 
0
source share

All Articles