C++ Primer 总结之Chap3 Library Types
2017-04-07 13:07
204 查看
本系列为本人温习C++基础时所记的tips,欢迎各位同学指正,共同进步TvT。
不可在header中加入
字符串常量(string literal)的类型是
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
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
Any variable used to store the result from the string size operation ought to be of type
The functions in cctype header test a character and return
用一个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().
Not every object in C++ has a name(like array elements), those have names are called varibale.
All of the library containers define iterator types, but only a few of them support subscript
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.
iterator 的赋值一旦超出 end() 就会报错。
Each container type also defines a type named
Iterators can NOT be added!!!
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.
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.
Reference : C++ Primer 4th edition(评注版)
不可在header中加入
usingdeclaration,必须用全称如
std::cin。因为如果加上去的话,you are placing a using declaration in every program that includes the header whether that program wants the using declaration or NOT.
字符串常量(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
Thestringinput 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
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
size_typeis 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
returnfrom size to an
int, which may be too small to store a big number. When we index a string, this rule also applys.
The functions in cctype header test a character and return
0if 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
An iterator is a type that lets us examine the elements in a container and navigate from one element to another.All of the library containers define iterator types, but only a few of them support subscript
[].
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.
iterator 的赋值一旦超出 end() 就会报错。
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.
Iterators can NOT be added!!!
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
The output ofbitsetis the same as our common idea, from higher order to lower order.
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(评注版)
相关文章推荐
- C++ Primer 总结之Chap7 Functions
- C++ Primer 总结之Chap5 Expressions
- C++ Primer 总结之Chap2 Variables and basic types
- C++ Primer 总结之Chap4 Arrays and Pointers
- C++ Primer总结
- C++ primer 4th 第10章《关联容器》总结
- 看《c++ primer》的过程中原来没遇到的内容的总结(会慢慢添加的)
- C++ Primer Chap13
- C++ primer 4th 第9章《顺序容器》总结-所有知识表格汇总
- C++ Primer阅读总结(未完!)
- c++ primer总结小点
- C++ Primer总结(一)
- 重温C++ primer 之const总结
- C++ primer 第三章总结 100-155
- 【C++ Primer】函数参数传递总结
- C++ primer学习: const 总结
- 看《c++ primer》的过程中原来没遇到的内容的总结(会慢慢添加的)
- 华三pap及chap验证实验总结
- 重温C++ primer 之重载符操作与转换总结
- 重温C++ primer 之const总结