STL 容器

std::unordered_set
std::unordered_set<int>
是一个不含重复元素的容器,其中的每一个元素都是独一无二的。
和unordersd_map类似这个unorder暗示着,这两个头文件中类的底层实现----Hash
std::pair
这个类主要功能是把两个不同类型或者相同类型的数据组合成一个数据。可以使用first、second来访问变量。
初始化一个pair可以使用构造函数,也可以使用std::make_pair函数,make_pair函数的定义如下:
template pair make_pair(t1 a, t2 b) { return pair(a, b); }

这有一个例子,如下

#include <iostream>
#include <utility>
#include <string>
using namespace std;

int main () {
pair <string,double> product1 ("tomatoes",3.25);
pair <string,double> product2;
pair <string,double> product3;

product2.first = "lightbulbs"; // type of first is string
product2.second = 0.99; // type of second is double

product3 = make_pair ("shoes",20.0);

cout << "the price of " << product1.first << " is $" << product1.second << "\n";
cout << "the price of " << product2.first << " is $" << product2.second << "\n";
cout << "the price of " << product3.first << " is $" << product3.second << "\n";
return 0;
}
其运行结果如下:

1 the price of tomatoes is $3.25
2 the price of lightbulbs is $0.99
3 the price of shoes is $20



Size_t和int区别

(1)size_t和int
size_t是一些C/C++标准在stddef.h中定义的。这个类型足以用来表示对象的大小。size_t的真实类型与操作系统有关。

在32位架构中被普遍定义为:

typedef   unsigned int size_t;

而在64位架构中被定义为:

typedef  unsigned long size_t;

size_t在32位架构上是4字节,在64位架构上是8字节,在不同架构上进行编译时需要注意这个问题。而int在不同架构下都是4字节,与size_t不同;且int为带符号数,size_t为无符号数
(2)ssize_t
ssize_t是有符号整型,在32位机器上等同与int,在64位机器上等同与long int.

(3)size_t和ssize_t作用
size_t一般用来表示一种计数,比如有多少东西被拷贝等。例如:sizeof操作符的结果类型是size_t,该类型保证能容纳实现所建立的最大对象的字节大小。 它的意义大致是“适于计量内存中可容纳的数据项目个数的无符号整数类型”。所以,它在数组下标和内存管理函数之类的地方广泛使用。

而ssize_t这个数据类型用来表示可以被执行读写操作的数据块的大小.它和size_t类似,但必需是signed.意即:它表示的是signed size_t类型的。