Вы находитесь на странице: 1из 3

/*

* File: 20160913_CPP_L04.cpp.
* Title: 2016-09-13 C++ Lesson 04
* Author: Renato Montes
* Description: Notes for Albert Wei's C++ class
*/

#include <iostream>
using namespace std;

int main() {
int m = 11, n = 12;
cin >> m >> n;
cout << m << ' ' << n << endl;
}
//when entering hello, it fails
//but n retains the previous value
//be careful: variable is set to zero only when the string is in a good state

/* The String class */


#include <string>
string s1, s2("hello"), s3(80, '*');
//s1 empty string
//s3: 80 '*'s
string s4(s2);
//the so-called copy-constructor (copy ctor), a constructor that takes another
// string

//the null character is not special in a string


//you can have a null character in the middle
//strings are not terminated by null characters

//you don't need strcmp


//can use <, <=, >, >=, ==, != to compare strings

//a lot of the time can mix C-style strings:


if (s2=="hello") {
...
}

//some other operations:


s2 + "world"; //doesn't change s2
s2 += "world";
s2[0]='H'; //change first character of the string to capital H

//some methods:
length(), size() //both return the length
//Albert always uses size()
//it turns out that a string is a container (a container of chars) & all
//containers have a size() method
//vector is another container

//max_size() returns the maximum possible size of a string


//9223372036854775807 !!!
//we can assume a line can be contained in it

//e.g.:
string word;
cin >> word; //try to read a word
while(cin >> word)
//process word

//processing a string character by character:


for (string::size_type i=0; i < s.size(); i++)
//a lot of the classes define a size_type
//vector has a size_type, which is its size
//this makes it more portable in different systems
//process s[i]

//e.g.: changing a string to all uppercase


void uppercase(string& s) {
//pass by reference with &, remember!
for (string::size_type i=0; sz = s.size(); i < sz; i++)
s[i] = toupper(s[i]);
}
//to include a C library:
//C: stdio.h C++:cstdio
//C: ctype.h C++:cctype

//C++11 has a simpler method


for (auto c : s) //range-based for-loop
//process c
//auto tells the compiler to infer the type of c
//this means you can write things like
auto n = 1;
//problem: c is a copy of s by value

//second version:
for (auto& c : s)
//process c
void uppercase(string& s) {
for(auto& c : s)
c = toupper(c);
}

/* Reading a line */
//getline takes three arguments:
getline(stream, str, delim)
//a stream (here istream), a string, and a delimiter (a char)
// (defaults to '\n', can write with
// just two arguments)
//keeps extrating characters from stream & storing them in _str_ until:
//1) end-of-file becomes true (sets eofbit)
//2) a char equal to delim has been extracted (this character is discarded)
//3) a failure case: str.max_size() characters have been stored (sets failbit)
//If no characters have been extracted, sets failbit as well.
//getline returns _stream_
//remember a stream can be used as true or false, and thus can put it in a
// while loop

//For simplicity, we assume 3) never happens

/* Standard idiom to process stdin line by line */


string line;
while (getline(cin, line))
//process line
/* istringstream */
#include <sstream>
//e.g.
istringstream iss(" 123abc456");
int n;
if (iss >> n)
cout << n << endl; //123
if (iss >> n)
cout << n << endl; //no output
string word;
if (iss >> word)
cout << word << endl; //no output: iss is not in a good state!
iss.clear(); /** curious syntax! */ //changes iss to a good state
if (iss >> word)
cout << word << endl; //abc456
iss.str("789"); //doesn't reset to good state
if (iss >> n)
cout << n << endl; //no output: eofbit is set
iss.clear();
if (iss >> n)
cout << n << endl; //789

//Example:
/* Summing integers obtained interactively */
string line;
int n, sum = 0;
while (1) {
cout << "Enter an integer: ";
if (!getline(cin, line)) {
cin.clear();
break;
}
istringstream iss(line);
if (iss >> n)
sum += n;
}
cout << sum << endl;

Вам также может понравиться