C++ Standard Library Strings

Table of contents:

Our early (pre-Qt) examples will make use of C++ Standard Library strings (see Appendix B). Standard Library strings have many disadvantages when compared to QStrings, but they are easy to use and have a similar public interface that includes many functions to construct and modify strings.

Example 1.14 demonstrates its basic usage.

Example 1.14. src/generic/stlstringdemo.cpp

#include 
#include 

int main() {
 using namespace std;
 string s1("This "), s2("is a "), s3("string.");
 s1 += s2; <-- 1
 string s4 = s1 + s3;
 cout << s4 << endl;

 string s5("The length of that string is: ");
 cout << s5 << s4.length << " characters." << endl;

 cout << "Enter a sentence: " << endl;
 getline(cin, s2);
 cout << "Here is your sentence: 
" << s2 << endl;
 cout << "The length of it was: " << s2.length() << endl;
 return 0;
}
 

(1)concatenation

Here is the compile and run:

src/generic> g++ -ansi -pedantic -Wall stlstringdemo.cpp
src/generic> ./a.out
This is a string.
The length of that string is 17
Enter a sentence:
20 years hard labour
Here is your sentence:
20 years hard labour
The length of it was: 20
src/generic>

Observe that we used the getline(istream, string) function to take a string from standard input stream. We will learn more about input from streams in the following section.


Streams

Part I: Introduction to C++ and Qt 4

C++ Introduction

Classes

Introduction to Qt

Lists

Functions

Inheritance and Polymorphism

Part II: Higher-Level Programming

Libraries

Introduction to Design Patterns

QObject

Generics and Containers

Qt GUI Widgets

Concurrency

Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments

Bibliography

MP3 Jukebox Assignments



An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net