容器

顺序容器

初始化

//初始化一个size为0的vector
vector<int> abc;

//初始化size,但每个元素值为默认值
vector<int> abc(10);    //初始化了10个默认值为0的元素

//初始化size,并且设置初始值
vector<int> cde(10,1);    //初始化了10个值为1的元素

int a[5] = {1,2,3,4,5};
//通过数组a的地址初始化,注意地址是从0到5(左闭右开区间)
vector<int> b(a, a+5);

vector<int> a(5,1);
//通过a初始化
vector<int> b(a);

vector容器的自增长

vector提供capacity和reserve成员,capacity()获取预存元素总数,reserve()设置预存数

  std::vector<int> myvector;
  for (int i=0; i<100; i++) myvector.push_back(i);

  std::cout << "size: " << (int) myvector.size() << '\n';  //100
  std::cout << "capacity: " << (int) myvector.capacity() << '\n'; //128
  
  myvector.reserve(200);
  std::cout << "size: " << (int) myvector.size() << '\n';  //100
  std::cout << "capacity: " << (int) myvector.capacity() << '\n'; //200

当vector容器不得不分配新的存储空间时,以加倍当前容量的策略重新分配内存

排序

//  通过年龄排序(升序)
bool SortByAge(Student &s1, Student &s2)
{
  return s1.age < s2.age;
}

//  通过名称排序(升序)
bool SortByName(Student &s1, Student &s2)
{
  return s1.name.compare(s2.name) < 0;
}

int main(void)
{
  std::vector<Student> list;
  Student *s1 = new Student(3, "ggg");
  Student *s2 = new Student(4, "aaa");
  Student *s3 = new Student(3, "ggg");
  list.push_back(*s1);
  list.push_back(*s2);
  list.push_back(*s3);
  std::sort(list.begin(), list.end(), SortByName);

  return 0;
}

去重复

#include "vector"
#include "algorithm"
#include "string"

class Student
{
public:
  int age;
  std::string name;

  Student(int age, const char * name)
  {
    this->age = age;
    this->name = name;
  }

  bool operator ==(Student& s)
  {
    if (this->age == s.age && this->name == s.name)
    {
      return true;
    }
    return false;
  }
};

int main(void)
{
  std::vector<Student> list;
  Student *s1 = new Student(4, "ggg");
  Student *s2 = new Student(4, "ggg");
  Student *s3 = new Student(3, "xyg");
  list.push_back(*s1);
  list.push_back(*s2);
  list.push_back(*s3);
  //std::sort(list.begin(), list.end());
  //   排序后才能去重
  list.erase(std::unique(list.begin(), list.end()), list.end());

  for (std::vector<Student>::iterator pos = list.begin(); pos != list.end(); ++pos)
  {
    printf("%s\n", pos->name.c_str());
  }
  
  getchar();
  return 0;
}

关联容器

通过键(key)高效的查找和读取元素

map<string , int >mapstring;         map<int ,string >mapint;
map<sring, char>mapstring;         map< char ,string>mapchar;

map<int ,string> maplive;  
maplive.insert(pair<int,string>(102,"aclive"));
maplive.insert(map<int,string>::value_type(321,"hai"));
maplive[112]="April";//map中最简单最常用的插入添加!

//查找
map<int ,string >::iterator l_it;; 
l_it=maplive.find(112);
if(l_it==maplive.end())
    cout<<"we do not find 112"<<endl;
else cout<<"wo find 112"<<endl;

容器适配器

适配器包括容器适配器、迭代器适配器、函数适配器

迭代器

迭代器定义和初始化

vector<int>    ivec;
vector<int>::iterator    iter1=ivec.bengin();    //将迭代器iter1初始化为指向ivec容器的第一个元素

vector<int>::iterator   iter2=ivec.end();    //将迭代器iter2初始化为指向ivec容器的最后一个元素的下一个

只要vector和deque支持-、>、>=、<、<=,因为只只有这两种容器支持快速、随机访问

iter1-iter2            //两个迭代器的减法,得出两个迭代器的距离。两个迭代器必须指向容器中的元素或超出容器末端的下一个元素

迭代器const_iterator

  每种容器还定义了一种名为const_iterator的类型。该类型的迭代器只能读取容器中的元素,不能用于改变其值。之前的例子中,普通的迭代器可以对容器中的元素进行解引用并修改,而const_iterator类型的迭代器只能用于读不能进行重写。例如可以进行如下操作:

for(vector<int>::const_iterator iter=ivec.begin();iter!=ivec.end();++iter)
     cout<<*iter<<endl;       //合法,读取容器中元素值

for(vector<int>::const_iterator iter=ivec.begin();iter!=ivec.end();++iter)
    *iter=0;        //不合法,不能进行写操作

  const_iterator和const iterator是不一样的,后者对迭代器进行声明时,必须对迭代器进行初始化,并且一旦初始化后就不能修改其值。这有点像常量指针和指针常量的关系。例如:

vector<int>    ivec(10);
const    vector<int>::iterator    iter=ivec.begin();
*iter=0;    //合法,可以改变其指向的元素的值
++iter;    //不合法,无法改变其指向的位置
posted @ 2019-08-18 03:02  熊云港  阅读(151)  评论(0编辑  收藏  举报