c ++ primer fifth edition of Chapter III

Chapter 3 strings, vectors and arrays

main content Features
string Variable-length strings
vector Variable-length collection
Array Similar vector
Iterator Similar pointer

using namespace statement 3.1

[!] Header files without using

using namespace std;//全部
using std::cin;//某个

3.2 Standard Library string type

#include<string>
using std::string;

3.2.1 define and initialize a string

string s1 ;
string s2 (s1); 
string s2 =s1; 
string s3 ("value") ;
string s3 = "value" ;
string s4(n,'c');

3.2.2 string operations on objects

os<<s;输出流如std::cout
is>>s;输入流如std::cin
getline (is, s);
s.empty ();
s.size ();
s[n];
s1+s2;
s1=s2;
s1==s2;
s1!=s2;
<, <=, >, >=;
string s;
while(cin>>s){}//非法输入结束
//保留空白字符
while(getline(cin,s)){
	if(!s.empty()){..}
	if(!s.size()>1){..}
}

//size()函数返回string::size_type类型的值,无符号数
operating Explanation
size () function Return value string :: size_type type unsigned number
+ Splicing and character string, both may have at least a string object

3.2.3 process character string object

#include<cctype>
//cname的头文件,属于std空间

Range for

string str="aaa";
for(auto c:str){
cout<<c<<endl;
}//c为char

Change the character

for (auto &c : s)   
	{c = toupper(c);}
cout << s << endl;

Subscript operator

输入参数:string::size_type|整数即可
返回:引用
string::size_type类型获取方法:decltype(s.size())
//例子
	string s="dwa";
	for(int i=0;i!=s.size();++i){
		cout<<s[i]<<endl;
	}

3.3 Standard Library type vector

#include<vector>
using std::vector//是一个类模板
vector<int> v1;
vector<Sales_item> v2;//<...>内不可以是引用,因为引用不是对象
vector<vector<int>> v3;

3.3.1 Definition and initialization vector objects

Support List initialized by default initialization, copy ...

3.3.2 add elements to the vector object

v.push_back(i);//添加到末尾

[!] Note that the range for, {...} statement can not have an internal additive element

3.3.3 Other vector operations

operating Explanation
v.size() Returns the vector <...> :: size_type type
v.empty()
v [n]
=
==,!=,<,>,<=,>=

Range for

//改变
for(auto &i:v){
...
}

[!] Vector-to-air may not add elements with subscript

3.4 Introduction iterator

Support iterator object
string
Standard Library containers

3.4.1 using an iterator

Iterator member Explanation
begin() Returns the first element iterator
end() Returns the next position of the last element

Container is empty, then begin = end is the same iterator

3.4.2 iterator operation

Operator (ITER iterators) Explanation
* process Although dereference operators, but returns the element referred to [reference]
iter-> members || (* iter). members Dereference get members
++,– A mobile unit
==,! =, +, -, Comparison Type is generally used auto,
(*iter).empty()
iter1-iter2 Return distance, either positive or negative, difference_type

[!] Dereference iterator must be legal for

Gets a reference type

string s("adada");
if(s.begin()!=s.end()){
	auto iter =s.begin();
	*iter=toupper(*iter);//*iter获取该元素的引用,可以对其修改成大写
}

for circulation! =, Because most of the library container iterator is not <operators

for(auto it=s.begin();it!=s.end();++it){
	...
}

Iterator type

Iterator type Function (read-write)
vector::iterator it1; r,w
string::iterator it2; r,w
vector::const_iterator it3; r
string::const_iterator it4; r

[!] Constant iterator must be const_iterator

[!] Return value of the begin and end of the container may be depending on, returns the constant const iterator, const iterator returns the contrary, if you want to return an iterator constants, C ++ 11 introduces cbegin (), cend ()

[!] Iterator's loop, the elements can not be added to the vessel

3.5 Array

Size is fixed

3.5.1 Definition and initialization of the built-in array

Dimension must be a constant expression

constexpr unsigned sz=10;
int *p[sz];//p是数组,元素是int指针

[!] The functions that define an array, initialized with default values ​​will contain undefined

An array of characters

char a1[]={'a','\0'};//长度是2
char a2[]="c";//长度是2,隐含一个'\0'

Can not copy assignment

//int a[]={0,1,2};
//int a2[]=a;//error
//a2=a;//error

An array of pointers

int arr[]={1,2,3};
int *p=arr;//p指向arr
cout<<*p<<endl;//1
//也可以写成
int (*p2)[3]=&arr;//p2指向arr
cout<<*(*p2)<<endl;//1
cout<<*p2<<endl;//0x61fe34
cout<<p2<<endl;//0x61fe34

Reference to the array

int arr[]={1,2,3};
int (&arr2)[3]=arr;
cout<<arr2[1]<<endl;//2

There are an array of pointer element

int *p[10];//p的元素是指针
int *(&p2)[10]=p;//p2是p的引用

3.5.2 Access array elements

Subscript type
size_t

Range for

for(auto i:array){}

3.5.3 Pointers and Arrays

Array name is the address of the first element, but also a pointer

int a[]={1,2,3};
auto a2(a);//a2是指向a的指针
decltype(a) a3={2,3,4};//但是decltype(a) 返回int[3]

Pointer also iterators

int a[]={1,2,3};
int *p=a;
++p;
cout<<*p<<endl;//2
int *end=&a[3];//尾后迭代器
--end;
cout<<*end<<endl;//3

Standard library functions begin, end

int a[]={1,2,3};
int *be=begin(a);
int *en=end(a);
cout<<*be<<" "<<*(en-1)<<endl;//1 3

Subscripts and pointer

	int a[]={1,2,3,4,5,6};
	int *p=&a[2];//*p是3
	int j=p[1];//相当于*(p+1),4
	int k=p[-2];//可正可负,1

3.5.4 C-style strings

#include<cstring>

Copy, splice c

	char s1[]="";
	char s2[]="wda";
	strcpy(s1,s2);//copy
	strcat(s1,"ss");//拼接
	cout<<s1<<endl;//wdass

Interface 3.5.5 with old code

C-style strings and string (the '\ 0' end of the char [])

	string s3=s1;//wdass,可以赋值
	s3+=s2;//"wdasswda",可以+,但是不能+两边都是char[]

[!] Can not be initialized with a string character pointer

String variable array of characters

//char *p=s3;//error
const char *p=s3.c_str();//p指向s3变成的char[]

Array initialization vector

	int a[]={1,2,3,44};
	vector<int> v1(begin(a)+1,end(a));//2 3 44

3.6 Multidimensional Arrays

#ifdef CONSTEXPR_VARS
	constexpr size_t rowCnt = 3, colCnt = 4;
#else
	const size_t rowCnt = 3, colCnt = 4;
#endif
	int ia[rowCnt][colCnt];  
    for (size_t i = 0; i != rowCnt; ++i) {
        for (size_t j = 0; j != colCnt; ++j) {         
     		...
		}
	}
//范围for,外层要用auto &
	for (const auto &row : ia) 
		for (auto col : row)  
			cout << col << endl; 

Multi-dimensional arrays and pointers

	int a[2][2]={{1,2},{3,4}};
	int (&a2)[2]=a[1];//a2[2]={3,4}

	int (*p)[2]=a;//*p={0,1}

Traversal

	#ifdef CONSTEXPR_VARS
	constexpr size_t rowCnt = 3, colCnt = 4;
#else
	const size_t rowCnt = 3, colCnt = 4;
#endif
	int ia[rowCnt][colCnt]; 
	for (auto p = ia; p != ia + rowCnt; ++p) {
		// q points to the first element of an array of four ints; 
		// that is, q points to an int
	    for (auto q = *p; q != *p + colCnt; ++q)
	        ...
		
	}
	for (auto p = begin(ia); p != end(ia); ++p) {
		// q points to the first element in an inner array
		for (auto q = begin(*p); q != end(*p); ++q)
			...
		
	}

Type alias

#ifdef TYPE_ALIAS_DECLS
	using int_array = int[4]; // new style type alias declaration 
#else
	typedef int int_array[4]; // equivalent typedef declaration
#endif
//int_array *p=ia跟int (*p)[4]=ia一样
	for (int_array *p = ia; p != ia + 3; ++p) {
	    for (int *q = *p; q != *p + 4; ++q)
	         cout << *q << ' ';
		cout << endl;
	}
Published 19 original articles · won praise 0 · Views 1253

Guess you like

Origin blog.csdn.net/qq_35459198/article/details/105315530