step1:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

main()
{
   vector<string> SS;

   SS.push_back("The number is 10");
   SS.push_back("The number is 20");
   SS.push_back("The number is 30");

   cout << "Loop by index:" << endl;

   int ii;
   ; ii < SS.size(); ii++)
   {
      cout << SS[ii] << endl;
   }

   cout << endl << "Constant Iterator:" << endl;

   vector<string>::const_iterator cii;
   for(cii=SS.begin(); cii!=SS.end(); cii++)
   {
      cout << *cii << endl;
   }

   cout << endl << "Reverse Iterator:" << endl;

   vector<string>::reverse_iterator rii;
   for(rii=SS.rbegin(); rii!=SS.rend(); ++rii)
   {
      cout << *rii << endl;
   }

   cout << endl << "Sample Output:" << endl;

   cout << SS.size() << endl;
   cout << SS[] << endl;

   swap(SS[], SS[]);
   cout << SS[] << endl;
}

step2:

Compile: g++ exampleVector.cpp
Run: ./a.out

Output:

Loop by index:
The number
The number
The number 

Constant Iterator:
The number
The number
The number 

Reverse Iterator:
The number
The number
The number 

Sample Output:

The number
The number 

此例子主要说明vector的使用。

编程语言方面:vector是C++标准模板库中的部分内容,中文偶尔译作“容器”,但并不准确。它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据

例子2:(作为2纬数组使用)

step1:

#include <iostream>
#include <vector>

using namespace std;

main()
{
   // Declare size of two dimensional array and initialize.
   vector< vector<, vector<,));    

   vI2Matrix[][] = ;
   vI2Matrix[][] = ;
   vI2Matrix[][] = ;
   vI2Matrix[][] = ;
   vI2Matrix[][] = ;
   vI2Matrix[][] = ;

   cout << "Loop by index:" << endl;

   int ii, jj;
   ; ii < ; ii++)
   {
      ; jj < ; jj++)
      {
         cout << vI2Matrix[ii][jj] << endl;
      }
   }
}
         

step2:

Compile: g++ exampleVector2.cpp
Run: ./a.out

Loop by index:

例子3:(多纬数组)

step1:

#include <iostream>
#include <vector>

using namespace std;

main()
{
   vector< vector< vector<, vector< vector<, vector<,)) );

   ; kk<; kk++)
   {
      ; jj<; jj++)
      {
         ; ii<; ii++)
         {
            cout << vI3Matrix[ii][jj][kk] << endl;
         }
      }
   }
}

step2:

[root@localhost stl_test0001]# g++ exampleVector3.cpp
[root@localhost stl_test0001]# ./a.out 

例子3:(Example of iterators used with a two dimensional vector)

step1:

#include <iostream>
#include <vector>

using namespace std;

main()
{
   vector< vector<int> > vI2Matrix;    // Declare two dimensional array
   vector<int> A, B;
   vector< vector<int> >::iterator iter_ii;
   vector<int>::iterator                 iter_jj;

   A.push_back();
   A.push_back();
   A.push_back();
   B.push_back();
   B.push_back();
   B.push_back();

   vI2Matrix.push_back(A);
   vI2Matrix.push_back(B);

   cout << endl << "Using Iterator:" << endl;

   for(iter_ii=vI2Matrix.begin(); iter_ii!=vI2Matrix.end(); iter_ii++)
   {
      for(iter_jj=(*iter_ii).begin(); iter_jj!=(*iter_ii).end(); iter_jj++)
      {
         cout << *iter_jj << endl;
      }
   }
}

step2:

Compile: g++ exampleVector2.cpp
Run: ./a.out

Using Iterator:

list使用:

step1:

// Standard Template Library example

#include <iostream>
#include <list>
using namespace std;

// Simple example uses type int

main()
{
   list<int> L;
   L.push_back();              // Insert a new element at the end
   L.push_front();             // Insert a new element at the beginning
   L.insert(++L.begin(),);     // Insert "2" before position of first argument
                                // (Place before second argument)
   L.push_back();
   L.push_back();

   list<int>::iterator i;

   for(i=L.begin(); i != L.end(); ++i) cout << *i << " ";
   cout << endl;
   ;
}

step2:

Compile: g++ example1.cpp
Run: ./a.out

Output:     
Standard Template Library example using a class.step1
// Standard Template Library example using a class.

#include <iostream>
#include <list>
using namespace std;

// The List STL template requires overloading operators =, == and <.

class AAA
{
   friend ostream &operator<<(ostream &, const AAA &);

   public:
      int x;
      int y;
      float z;

      AAA();
      AAA(const AAA &);
      ~AAA(){};
      AAA &operator=(const AAA &rhs);
      int operator==(const AAA &rhs) const;
      int operator<(const AAA &rhs) const;
};

AAA::AAA()   // Constructor
{
   x = ;
   y = ;
   z = ;
}

AAA::AAA(const AAA &copyin)   // Copy constructor to handle pass by value.
{
   x = copyin.x;
   y = copyin.y;
   z = copyin.z;
}

ostream &operator<<(ostream &output, const AAA &aaa)
{
   output << aaa.x << ' ' << aaa.y << ' ' << aaa.z << endl;
   return output;
}

AAA& AAA::operator=(const AAA &rhs)
{
   this->x = rhs.x;
   this->y = rhs.y;
   this->z = rhs.z;
   return *this;
}

int AAA::operator==(const AAA &rhs) const
{
   ;
   ;
   ;
   ;
}

// This function is required for built-in STL list functions like sort
int AAA::operator<(const AAA &rhs) const
{
   ;
   ;
   ;
   ;
}

main()
{
   list<AAA> L;
   AAA Ablob ;

   Ablob.x=;
   Ablob.y=;
   Ablob.z=4.2355;
   L.push_back(Ablob);  // Insert a new element at the end

   Ablob.x=;
   L.push_back(Ablob);  // Object passed by value. Uses default member-wise
                        // copy constructor
   Ablob.z=3.2355;
   L.push_back(Ablob); 

   Ablob.x=;
   Ablob.y=;
   Ablob.z=7.2355;
   L.push_back(Ablob); 

   list<AAA>::iterator i;

   for(i=L.begin(); i != L.end(); ++i) cout << (*i).x << " "; // print member
   cout << endl;      

   for(i=L.begin(); i != L.end(); ++i) cout << *i << " "; // print with overloaded operator
   cout << endl;

   cout << "Sorted: " << endl;
   L.sort();
   for(i=L.begin(); i != L.end(); ++i) cout << *i << " "; // print with overloaded operator
   cout << endl;

   ;
}

step2:

Output:

  4.2355
   4.2355
   3.2355
   7.2355

Sorted:
  7.2355
   3.2355
   4.2355
   4.2355

C++ 函数模板

step1:

#include <iostream>
using std::cout;
using std::endl;

template<class T> T max(const T* data, int size) {
    T result = data[];
     ; i < size ; i++)
      if(result < data[i])
        result = data[i];
    return result;
  }

template<class T> T min(const T* data, int size) {
    T result = data[];
     ; i < size ; i++)
      if(result > data[i])
        result = data[i];
    return result;
  }

int main() {
  double data[] = {1.5, 4.6, 3.1, 1.1, 3.8, 2.1};
  , , , , , , , };

  ];
  cout << "Minimum double is " << min(data, dataSize) << endl;
  cout << "Maximum double is " << max(data, dataSize) << endl;

  ];
  cout << "Minimum integer is " << min(numbers, numbersSize) << endl;
  cout << "Maximum integer is " << max(numbers, numbersSize) << endl;

  ;
}

step2:

g++ random_shuffle22.cpp
[root@localhost stl_test0001]# ./a.out
Minimum double is 1.1
Maximum double is 4.6
Minimum integer
Maximum integer 

参考:

http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html

http://www.cnblogs.com/shixinzhu/archive/2012/03/05/2380203.html

https://www.sgi.com/tech/stl/download.html

Linux环境下stl库使用(vector)的更多相关文章

  1. Linux环境下stl库使用(map)

    例子1: testMap.cpp #include <string.h> #include <iostream> #include <map> #include & ...

  2. PCL库在Linux环境下的编译安装

    PCL库在Linux环境下的编译安装 PCL库的源码库:https://github.com/PointCloudLibrary/pcl 下载完了之后解压下来 编译库的几个步骤 mkdir build ...

  3. linux环境下学习使用pro*c/c++工具

    1.proc是oracle用来预编译嵌入SQL语句的c程序. 2.如何使用proc工具 在Linux环境下,首先确保gcc编译器正常使用,安装oracle数据库或者客户端,一般就会默认安装pro*c/ ...

  4. Linux环境下段错误的产生原因及调试方法小结(转)

    最近在Linux环境下做C语言项目,由于是在一个原有项目基础之上进行二次开发,而且 项目工程庞大复杂,出现了不少问题,其中遇到最多.花费时间最长的问题就是著名的“段错误”(Segmentation F ...

  5. Linux环境下段错误的产生原因及调试方法小结

    转载自http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之 ...

  6. 【转】【调试技巧】Linux环境下段错误的产生原因及调试方法小结

    本文转自:http://www.cnblogs.com/panfeng412/archive/2011/11/06/segmentation-fault-in-linux.html 1. 段错误是什么 ...

  7. 多线程编程之Linux环境下的多线程(一)

    一.Linux环境下的线程 相对于其他操作系统,Linux系统内核只提供了轻量级进程的支持,并未实现线程模型.Linux是一种“多进程单线程”的操作系统,Linux本身只有进程的概念,而其所谓的“线程 ...

  8. linux环境下验证码不显示的几种情况

    linux环境下验证码不显示的几种情况 gd库扩展没有安装. 查看phpinfo(),看看有没有安装gd库 yum安装gd库或者phpize安装 安装完成后记得重启php-fpm bom头的原因 在生 ...

  9. mosquitto在Linux环境下的部署/安装/使用/测试

    mosquitto在Linux环境下的部署 看了有三四天的的源码,(当然没怎么好好看了),突然发现对mosquitto的源码有了一点点感觉,于是在第五天决定在Linux环境下部署mosquitto. ...

随机推荐

  1. django的分页--不全也未实现

    一.Django内置分页 Paginator 二.自定义分页 分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该在数据库表中的起始位置. 1.设定每页显示数据条数 2.用户输 ...

  2. wordpress迁移到本地404错误

    线上wordpress迁移到本地可能出现找不到页面问题,可能是因为配置了url静态化,同时wordpress的文章链接是存在数据库中的 下面替换掉原来线上的文章链接,其中数据库名为wordpress ...

  3. Qt5.4 MSVC mysql驱动编译;

    http://www.mysql.com/ 下载mysql http://download.qt.io/archive/qt/     Qt 下载 1. mysql安装源码 注意勾选这一步,将mysq ...

  4. 手动fsck修复

    [转自]http://blog.chinaunix.net/uid-26719405-id-3781541.html 由于硬盘常年读写,系统会造成系统文件损坏,导致重启后无法登陆到系统, fsck不仅 ...

  5. 解读Unity中的CG编写Shader系列三

    转自http://www.itnose.net/detail/6096068.html 在上一个例子中,我们得到了由mesh组件传递的信息经过数学转换至合适的颜色区间以颜色的形式着色到物体上.这篇文章 ...

  6. Unity3d《Shader篇》绘制圆角图片

    Pass { CGPROGRAM // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a s ...

  7. 3.saltstack的grains和pillar学习笔记

    作者:刘耀 QQ:22102107 SaltStack_Grains Grains grains是minion第一次启动的时候采集的静态数据,可以用在salt的模块和其他组件中.其实grains在每次 ...

  8. nyoj123_士兵杀敌(四)_树状数组_插线求点

    士兵杀敌(四) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战 ...

  9. 【python】lxml

    来源:http://lxml.de/tutorial.html lxml是python中处理xml的一个非常强大的库,可以非常方便的解析和生成xml文件.下面的内容翻译了链接中的一部分 1.生成空xm ...

  10. 9.27js拓展、bootstrap菜鸟教程

    js(点击挂上 与 点击移除) <div id="dd" style="width:200px; height:200px; background-color:#6 ...