C++ Primer 总结之Chap3 Library Types

本系列为本人温习C++基础时所记的tips,欢迎各位同学指正,共同进步TvT。

  1. 不可在header中加入using declaration,必须用全称如std::cin。因为如果加上去的话,you are placing a using declaration in every program that includes the header whether that program wants the using declaration or NOT.

  2. 字符串常量(string literal)的类型是const char[ ],而不是string

String

//Ways to initialize a string
string s1;//default constructor, empty string
string s2(s1);
string s3("hello");
string s4(n,'c');//n copies of 'c'

cin >> s1 >> s2;//input "  Hello world", 
            //s1:"Hello", s2:"world"

string s5 = "Hello " + "world";//error
string s6 = s3 + " worl" + "d";//ok, left to right

Input

  1. The string input operator:

    • reads and discards any leading whitespaces
    • reads characters until the next whitespace encountered
    • so it reads word, and if you enter newlines directly, it seems you haven’t enter anything
    • whitespaces include spaces, newlines and tabs
  2. getline(cin,str)

    • reads the next line of input from stream and stores not including the newline. Also, it doesn’t ignore leading whitespaces. If you enter a newline directly, it just becomes an empty string
    • the first argument may be other input modes
  3. size_type is defined as a synonym for an unsigned type.

    Any variable used to store the result from the string size operation ought to be of type string::size_type. It is particularly important NOT to assign the return from size to an int, which may be too small to store a big number. When we index a string, this rule also applys.

  4. The functions in cctype header test a character and return 0 if the test fails, and return a non-zero value(not guaranteed to be greater than 0) when succeeds.

    isalpha(c);//true if letter
    isalnum(c);//true if letter or digit
    iscntrl(c);//true if control character
    isdigit(c);
    isupper(c);
    toupper(c);
    ispunct(c);//true if punctuation
    isgraph(c);//true if printable but not a space
    isprint(c);//printable
    isspace(c);//whitespace
    
    string str = "kfjJIIaa";
    cout << count_if(str.begin(), str.end(), isupper);
    transform(str.begin(), str.end(), str.begin(), toupper);
    
    string str2;
    cout << str2[0];//pos==size(), null character returned
    
    string str3 = "j\0j";
    cout << str3;//output:j
    string str4="jaj";
    str4[1]='\0';
    cout<<str4;//output:j j
    
    
    

    用一个C字符串赋值给str3的时候只有依靠\0才能知道其长度,所以实际上str3接收到的就只有j;而string本身是不会特殊看待’\0’的(见str4)

    When using [] operator, if pos == size(), a reference to the character with value CharT() (the null character) is returned.
    For the non-const version, the behavior is undefined if this character is modified to any value other than charT().

Vector

#include<vector>

vector<int> v1;
vector<int> v2(v1);
vector<int> v3(10);//10 copies of 0
vector<int> v4(10,8);//10 copies of 8

for(vector<int>::size_type i = 0; i != v4.size(); ++i)
  cout << v4[i] << endl;
v2 = v3;//Can be assigned directly

vector<int>::iterator it1 = v1.begin();
vector<int>::iterator it2 = v1.end();
it1 == it2;//true for empty vector

//const iterator vs const_iterator(iterator to const)
const vector<int>::iterator it3 = v3.begin();
*it3 = 4;//ok
++it3;//error

vector<int>::const_iterator it4 = v3.begin();
*it4 = 5;//error, read-only
++it4;//ok

const vector<int> v5(10);
vector<int>::iterator it5 = v5.begin();//error
v5.push_back(3);//error
(v1.begin() + v1.end() ) / 2;//Wrong!!! Interators can't add

Not every object in C++ has a name(like array elements), those have names are called varibale.

Iterator

  1. An iterator is a type that lets us examine the elements in a container and navigate from one element to another.

  2. All of the library containers define iterator types, but only a few of them support subscript [].

  3. When the vector is empty, the iterator returned by begin() is the same as that returned by end(). The iterator returned by end() can not be dereferenced or incremented, but decreased is possible when the vector is not empty.

  4. iterator 的赋值一旦超出 end() 就会报错。

  5. Each container type also defines a type named const_iterator, which should be used when reading but not writing to the container elements. When we dereference a const_iterator, the value returned is const. A plain iterator can not be of a vector that is const.

  6. Iterators can NOT be added!!!

  7. Any operation that changes the size of a vector makes existing iterators invalid. If you want to remember an element and the size of that vector may be changed, you should use subscript.

Bitset

  1. The output of bitset is the same as our common idea, from higher order to lower order.

  2. If the number of bits in the initializer is too big, the higer order bits will be discarded; if it’s too small, 0 will be placed on higer order bits.

#include<bitset>
bitset<8> b1;//00000000
bitset<8> b2(0xf);//00001111
bitset<8> b3("1101");//00001101
string str("1110001100");
bitset<8> b4(str, 3, 4);//00000001
bitset<8> b5(str, str.size() - 4);//00001100
bitset<8> b6("10086");//wrong

bitset<8> b;
b.any();b.none();b.filp();//change all
b.count();b.test(pos);b.reset();//turn off all
b.set();b.set(pos);//turn on
b.reset(pos);b.flip(pos);
b.to_ulong();// to_ulong()可用于与整数的位操作配合翻转之类的动作。

Reference : C++ Primer 4th edition(评注版)

猜你喜欢

转载自blog.csdn.net/niyiweia/article/details/69525875