对容器的一些操作

Xredman posted @ Jun 11, 2009 06:35:48 AM in 以前博文 , 950 阅读

 

        vector附初值vector<int> ivec(vec_size, 100);
        list   附初值list<int> ilist(list_size, 11);
        list类型的重新设置容器长度 ilist.resize(2 * ilist.size());
    容器类型是支持一组关系操作符的,可以用来比较二个容器大小(<,>,=)。不过,对于能够定义的容器的类型有三个限制
    <1>.元素类型必须支持等于操作符
    <2>.元素类型必须支持小于操作符
    <3>.元素类型必须支持一个缺省值。
    迭代器(iterator)提供一种一般化的方法,对顺序或关联容器类型中的每个元素进行连续访问。
    通过数组实现vector的初始化:
    int cc[4] = {1, 2, 3, 4};
    vector<int> ivec(cc, cc + 4);第二个指针指向的对象不包含在要被拷贝或遍历的元素之中。
 
    任意位置插入元素:
ivec.insert(ivec.begin(), 100);
    更加随机化的插法: 
q = find(ivec.begin(), ivec.end(), 5);
ivec.insert(q, 99);
第一个参数是一个位置,第二个参数是将要被插入的值,这个值将被插入到有iterator指向的位置的前面。
在某个位置插入指定数量的元素:
    ivec.insert(ivec.begin(), 10, -1);在begin()位置插入10个-1
向容器插入一段范围内的元素:
    ivec.insert(ivec.begin(),cc, cc + 4);
    泛型算法:把所有容器类型的公共操作抽取出来,形成一个通用算法集合,它能够被应用到全部容器类型以及内置数组类型上。





  • 无匹配
  • 无匹配
Jammu and Kashmir 9 说:
Sep 09, 2023 10:56:09 PM

Jammu and Kashmir State Board of School Education (JKBOSE) Going to Conducts Class (High School Level) Public Exam 2024, JKBOSE Exam will be Conduct Every Year on April of Month, Every year more than Lakhs of Students Appeared in this Exam, JKBOSE 9th Class Exam Dates Expected to be Announce Soon. In this Article,you will get JKBOSE Class Question Paper 2024 for Dogri, Science, Maths Jammu and Kashmir 9th Question Paper 2024 History, Geography, Hindi All Subjects. These Question Paper are Based on Class Prescribed by JKBOSE, J&K Class Model Paper 2024 is Very Important for Public Exam Preparation,Each Student adopts All sorts of ways to get a good Score in the Central Board Exam.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter