1 / 15

Principles of Computer Science I Honors Section

Principles of Computer Science I Honors Section. Note Set 10 CSE 1341. Today. String Streams. What’s the difference between:. 255 and “255” ??????????. Background. Web Cam pictures. Rover Communcations. VAB. Mission Control ( YOU ). Background.

milica
Download Presentation

Principles of Computer Science I Honors Section

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Principles of Computer Science IHonors Section Note Set 10 CSE 1341

  2. Today String Streams

  3. What’s the difference between: 255 and “255” ??????????

  4. Background Web Cam pictures Rover Communcations VAB Mission Control (YOU)

  5. Background • Communication in character strings • s 1 255 • Sent to the robot as a string of characters • Problem: • You have to build the string of characters before you can send it to the robot • You have to interpret the string of characters from the VAB worksheet before you can use them in calculations

  6. Numbers to Strings • Difficult/annoying with c-strings (null terminated char arrays) • Difficult to append a number to a c-string or stl-string //Can’t Do this string s; s = 32; cout << s; //WILL NOT display 32a //Can’t Do this either char str[25]; strcpy(str, 32); //WILL NOT COMPILE cout << s;

  7. c-way or old way • atoi(const char*), atof(const char*)......... • will convert an ascii string of digits to an integer • have to worry about c-string being properly formatted • What if result is too big to fit in an integer??

  8. Remember cout??? • You can send any fundamental data type to cout //This is OK! cout << 32; cout << “32”; cout << “go forward”; cout << 3.456; Can we do something similar, but store the data in a string instead of sending it to the screen?

  9. String Stream • string stream objects handle (behind the scenes) conversion of data types • Can insert or extract information from a string stream using >> and << • Stored in a String behind the scenes stringstream ss; istringstream iss; ostringstream oss;

  10. Example #include <sstream> #include <iostream> int main() { stringstream s; int x = 25; double y = 2.34; s << x; s << “ “; s << y; string str; s >> str; //like cin, but source is s cout << str; //What will this print??? return 0; }

  11. Example stringstream ss; ss << “1 2 3 4 5 6”; int temp; for (int i = 0; i < 6; i++) { ss >> temp; cout << temp * 2; } 2 4 6 8 10 12

  12. Example stringstream ss; char str[] = “1 2 3 4 5 6”; ss << str; int temp; for (int i = 0; i < 6; i++) { ss >> temp; cout << temp * 2; } 2 4 6 8 10 12

  13. Functions • str() • return a stl-string copy of what’s in the stringstream • getline(…) • used as if you were getting a line from cin

  14. Example stringstream ss; string temp; ss << "C++ is the best ever!"; ss >> temp; cout << temp; cout << endl; getline(ss, temp); cout << temp; C++ is the best ever!

  15. Questions??? ?

More Related