第4章 Container

news/2025/2/21 2:39:15/

第4章 Container

reference 引用

Declaring references

Reference is a new way to manipulate objects in C++

– char c; // a character

– char* p = &c; // a pointer to a character

– char &r = c; // a reference to a character

区分指针*:

`int* p = &c; //标点,不参与计算,表示作用

*p = c;` //运算符,参与计算,计算后有结果

同理区分引用&

– char* p = &c; // 运算符,取c的地址

– char& r = c; // 标点,表示r是引用类型。等号右边必须是变量,而不是常量(变量的值,只能被读取,不能被写入)。r表示c的别名

    int main(){char c = 'A';char& r = c;r = 'B';cout << "c:" << c << endl;cout << "r:" << r << endl;c = 'C';cout << "c:" << c << endl;cout << "r:" << r << endl;}/*输出c: Br: Bc: Cr: C*/

• Local or global variables

– type& refname = name;

– For ordinary variables, the initial value is required

• In parameter lists and member variables

– type& refname

– Binding defined by caller or constructor

References

• Declares a new name for an existing object 
int X = 47; 
int& Y = X; // Y is a reference to X 
// X and Y now refer to the same variable 
cout << "Y = " << y; // prints Y = 47 
Y = 18; 
cout << "X = " << x; // prints X = 18

Rules of references

• References must be initialized when defined

• Initialization establishes a binding

• In declaration

int x = 3;

int& y = x;

const int& z = x; z不能被修改,x可以被修改

• As a function argument

void f ( int& x ); f(y); // initialized when function is calledvoid f(int& x){x++;cout << "in f() x = " << x << endl;}int main(){int a = 51;cout << "b4 f() a =" << a << endl;f(a);cout << "after f() a =" << a << endl;}/*输出:b4 f() a = 51in f() x = 52after f() a = 52把a传入后,x就是a的别名,对x操作就是对a操作,所以a的值会发生变化*/

const作用:

void f(const int& x){x++;cout << "in f() x = " << x << endl;}
int main(){int a = 51;cout << "b4 f() a =" << a << endl;f(a);cout << "after f() a =" << a << endl;
}
//编译后会错误,不能给x++,因为x是const,不能被修改

• Bindings don’t change at run time, unlike pointers

• Assignment changes the object referred-to

int& y = x;

y = 12; // Changes value of x

• The target of a reference must have a location!

void func(int &x);

func (i * 3); // Warning or error!

因为i * 3结果是右值,不能赋给左值x

void f(int& x){x++;cout << "in f() x = " << x << endl;}
int main(){int a = 51;cout << "b4 f() a =" << a << endl;f(a + 2);cout << "after f() a =" << a << endl;
}/*
f(a+2)不能传给void f(int& x),会编译错误
如果改成void f(int x)可以编译通过
*/

Pointers vs. References

References

• can’t be null

• are dependent on an existing variable, they are an alias(别名) for an variable

• can’t change to a new “address” location

• Pointers

– can be set to null

– pointer is independent of existing objects

– can change to point to a different address

Restrictions

• No references to references 没有引用的引用

• No pointers to references 没有指向引用的指针

int&* p; // illegal *号离p近说明是指针

– Reference to pointer is ok 指针的引用可以

void f(int*& p);

• No arrays of references没有引用的数组

Container

A personal notebook

• It allows notes to be stored.

• It has no limit on the number of notes it can

store.

• It will show individual notes.

• It will tell us how many notes it is currently

storing.

Collection

• Collection objects are objects that can store an

arbitrary number of other objects.

What is STL

• STL = Standard Template Library

• Part of the ISO Standard C++ Library

• Data Structures and algorithms for C++.

Why should I use STL?

• Reduce development time.

–Data-structures already written and debugged.

• Code readability

–Fit more meaningful stuff(东西) on one page.

• Robustness 健壮性

–STL data structures grow automatically.

• Portable code. 可移植性

• Maintainable code 可维护代码

• Easy

C++ Standard Library

• Library includes:

–A Pair class (pairs of anything, int/int, int/char, etc)

–Containers

• Vector (expandable array) 扩展数组

• Deque (expandable array, expands at both ends)

• List (double-linked)

• Sets and Maps

–Basic Algorithms (sort, search, etc)

• All identifiers in library are in std namespace :

using namespace std;

The three parts of STL

• Containers

• Algorithms

• Iterators

The ‘Top 3’ data structures

• map

–Any key type, any value type.

–Sorted.

• vector

–Like c array, but auto-extending.

• list

–doubly-linked list

All Sequential Containers

• vector: variable array

• deque: dual-end queue

• list: double-linked-list

• forward_list: as it

• array: as “array”

• string: char. array

Example using the vector class

 #include <iostream>#include <vector>//Use “namespace std” so that you can refer to vectors in C++ libraryusing namespace std;int main(){//Declare a vector of ints(no need to worry about size)vector<int> x;//Add elementsfor(int a = 0; a < 1000; a++){a.push_back(a);}//Have a pre-defined iterator for vector,can use it to print out thr items in vectorvector<int>::iterator p;for(p = x.begin(); p < x.end(); p++){cout << *p << " ";//能用*因为重载了*运算符}return 0;}#include <iostream>#include <vector>//Use “namespace std” so that you can refer to vectors in C++ libraryusing namespace std;int main(){vector<int> x;for(int i = 0; i < 100; i++){x.push_back(i);}for(auto i: x){cout << i << " ";}return 0;}

generic classes泛型类

vector notes;

• Have to specify two types: the type of the collection itself (here: vector) and the type of the elements that we plan to store in the collection

(here: string)

vector

• It is able to increase its internal capacity as required: as more items are added, it simply makes enough room for them.

• It keeps its own private count of how many items it is currently storing. Its size method returns the number of objects currently stored in it.

• It maintains the order of items you insert into it. You can later retrieve them in the same order.

Basic Vector Operations

• Constructors

vector c;

vector c1(c2); 用c2创建c1

• Simple Methods

V.size( ) // num items

V.empty( ) // empty?

==, !=, <, >, <=, >=

V.swap(v2) // swap

• Iterators

I.begin( ) // first position

I.end( ) // last position

• Element access

V.at(index)

V[index]

V.front( ) // first item

V.back( ) // last item

//可以这么遍历
for(int i = 0; i < x.size(); i++){cout << x[i] << " ";
}

• Add/Remove/Find

V.push_back(e)

V.pop_back( )

v.insert(pos, e)

V.erase(pos)

V.clear( )

V.find(first, last, item)

Two ways to use Vector

Preallocate

vector<int> v(130);//
v[80] = 1;//ok
v[200] = 1;//bad

Grow tail

vector<int> v2;
int i;
while(cin >> i){v.push_back(i);
}

List Class

• Same basic concepts as vector

–Constructors

–Ability to compare lists (==, !=, <, <=, >, >=)

–Ability to access front and back of list

x.front(), x.back()

–Ability to assign items to a list, remove items

x.push_back(item)

x.push_front(item)

x.pop_back()

x.pop_front()

x.remove(item)

Sample List Application

#include <iostream>
#include <list>
#include <string>
using namespace std;int main(){//Declare a list of stringslist<string> s;s.push_back("hello");s.push_back("world");s.push_front("tide");s.push_front("crimson");s.push_front("alabama");list<string>::iterator p;for(p = s.begin(); p != s.end(); p++){cout << *p << " ";}cout << endl;
}
/*
输出:
alabama crimson tide hello worldNote the termination condition for our iterator p != s.end( ) 
–Cannot use p < s.end( ) as with vectors, as the list elements may not be stored in order
*/

Example of Lists

list<int> L;
for(int i = 1; i  <= 5; i ++){L.push_back(i);
}
//delete second item
L.erase(++L.begin());
copy(L.begin(), L.end(), ostream_iterator<int>(cout, ","));//Prints:1, 2, 3, 5

Maintaining an ordered list

  #include <iostream>#include <list>#include <string>using namespace std;int main(){list<string> s;string t;list<string>::iterator p;for(int a = 0; a < 5; a++){cout << "enter a string:" ;cin >> t;p = s.begin();while(p != s.end() && *p < t){p++;}s.insert(p, t);}for(p = s.begin(); p != s.end(); p++){cout << *p << " ";}cout << endl;}

Maps

• Maps are collections that contain pairs of values.

• Pairs consist of a key and a value.

• Lookup works by supplying a key, and

retrieving a value.

• An example: a telephone book

Example Map Program

   #include <map>#include <string>map<string, float> price;price{"snapple"} = 0.75;price{"coke"} = 0.50;string item;double total = 0;while(cin >> item){total += price{item};}

Simple Example of Map

map<long, int> root;
root[4] = 2;
root[1000000] = 1000;
long l;
cin >> l;
if(root.count(l)){//如果在映射容器中存在带有键K的元素,则该函数返回1。如果容器中不存在键为K的元素,则返回0cout << root[l];
}else{cout << "Not perfect square";
}

Iterator

• Declaring

list::iterator li;

• Front of container

list L;

li = L.begin();

• Past the end

li = L.end();

• Can increment

list::iterator li;

list L;

li=L.begin();

++li; // Second thing;

• Can be dereferenced

*li = 10;

Algorithms

• Take iterators as arguments

list<int> L;
vector<int> V;
// put list in vector
copy( L.begin(), L.end(), V.begin() )

List Example Again

list<int> L; 
for(int i=1; i<=5; ++i)L.push_back(i);
//delete second item.
L.erase( ++L.begin() );
copy( L.begin(). L.end(),ostream_iterator<int>(cout,,"));
// Prints: 1,2,3,5

for-each loop

A for-each loop iterates over the elements of arrays,vectors,or any other data sets.It assigns the value of the current element to the variable iterator declared inside the loop

for(type variable_name array/vector_name){loop statements
···
}

for-each循环,不能修改值,只能读取值

Example of for-each

 #include<iostream>using namespace std;int main()int arr[]=(1,2,3,4,5);//array initializationcout<<"The elements are:"for(int i arr){//autocout << i << " ";}return 0;}#include<iostream>#include<vector>using namespace std;int main(】vector<int>vec=(11,22,33,44,S5,66}:cout<<"The elements are:"for(auto var: vec){cout << var << " ";}return 0;}

Pros and Cons 利弊

Advantages of foreach loop

o It eliminates the possibility of errors and makes the code more readable.

o Easy to implement

o Does not require pre-initialization of the iterator

Disadvantages of foreach loop

o Cannot directly access the corresponding element indices

o Cannot traverse the elements in reverse order

o It doesn’t allow the user to skip any element as it traverses over each one of them

Using your own classes in STL Containers

• Might need:

–Assignment Operator, operator=()

–Default Constructor

• For sorted types, like map<>

–Need less-than operator: operator<()

• Some types have this by default:

–int, char, string

• Some do not:

–char *

The type in containers

What’s the difference?

vectorv1;

vector<Student&>v2;

vector<Student*>v3;

Pitfalls 陷阱/隐患

• Accessing an invalid vector<> element.

vector v;

v[100]=1; // Whoops!

Solutions:

–use push_back()

–Preallocate with constructor.

–Reallocate with reserve()

–Check capacity()

• Inadvertently inserting into map<>.

if (foo[”bob”]==1) //silently created entry “bob”

Use count() to check for a key without creating a new entry.

if ( foo.count(“bob”) )

• Not using empty() on list<> .

–Slow

if ( my_list.count() == 0 ) { … }

–Fast

if ( my_list.empty() ) {…}

• Using invalid iterator

list<int> L;
list<int>::iterator li;
li = L.begin();
L.erase(li);
++li; // WRONG
• Use return value of erase to advance 
li = L.erase(li); // RIGHT

http://www.ppmy.cn/news/96419.html

相关文章

C#调用FreeSpire.PDF获取PDF文档中使用的字体

除了图片之外&#xff0c;电子文件中使用的字体都必须要在本机中安装才能正常查看文字&#xff08;word缺少字体的话会自动使用相似或默认字体&#xff09;&#xff0c;要想知道电子文件中使用的字体&#xff0c;可以将电子文件转换为PDF文件&#xff08;如果是打印成PDF的话&a…

C++环形缓冲区设计与实现:从原理到应用的全方位解析

C环形缓冲区设计与实现&#xff1a;从原理到应用的全方位解析 一、环形缓冲区基础理论解析&#xff08;Basic Theory of Circular Buffer&#xff09;1.1 环形缓冲区的定义与作用&#xff08;Definition and Function of Circular Buffer&#xff09;1.2 环形缓冲区的基本原理&…

Visual Studio2022编译器实用调试技巧

目录 1.什么是bug 2.调试是什么&#xff1f; 3.debug和release的介绍 4.windows环境调试介绍 4.1 调试环境的准备 4.2 学会快捷键 4.3 调试的时候查看程序当前信息 4.4 查看内存信息 5.如果写出好&#xff08;易于调试&#xff09;的代码 7.编程常见的错误 1.什么是b…

Day2:Windows网络编程-TCP

今天开始进入Windows网络编程的学习&#xff0c;在学习的时候总是陷入Windows复杂的参数&#xff0c;纠结于这些。从老师的讲解中&#xff0c;这些内容属于是定式&#xff0c;主要学习写的逻辑。给自己提个醒&#xff0c;要把精力放在正确的位置&#xff0c;不要无端耗费精力。…

指针数组和数组指针

//void test(int arr[]) //{ // int sz sizeof(arr) / sizeof(arr[0]); // printf("%d\n", sz);//计算的是 // //地址除元素的大小的值 32位 ֵ值为1 64位 值为2 //} //int main() //{ // int arr[10] { 0 }; // test(arr); // char ch w; // …

String类的学习笔记(下):字符串拼接以及StringBuilder和StringBuffer的学习

本文介绍了String类对字符串进行拼接的方法 和拼接字符串的效率分析 以及能对字符串内容进行修改的StringBuilder和StringBuffer类其常用方法和区别 , 最后介绍了两个字符串经典面试题 StringBuilder和StringBuffer的学习 一.String类概括二.StringBuilder和StringBuffer1.字符…

第一节 Python 顺序结构

1 . Python 介绍 1.1 Python是什么 Python是一种高级编程语言,具有简单易学、功能强大、可扩展性好等特点,是目前广泛应用于Web开发、数据分析、人工智能等领域的编程语言之一。 Python的历史可以追溯到1989年,当时在荷兰的Guido van Rossum开发了这种语言,Guido最初设计P…

Arduino学习

物联网学习资料 《arduino程序设计基础》陈吕洲 北京航空航天大学出版社 半颗心脏博客导航一站式搜索(所有博客的汇总帖) Ai-Thinker 安信可科技 github 半颗心脏 | 徐宏 蓝牙技术 蓝牙网关【【智能家居】入门攻略二&#xff01;啥是网关&#xff1f;蓝牙、zigbee协议详…