C++ vector 使用笔记

map 插入 vector

#include <string>   

#include <iostream>   

#include <algorithm>   

#include <map>   

#include <vector>   

using namespace std;   

typedef map<string, string> STRING2STRING;   

typedef std::map<string, vector<string > > STRING2VECTOR;   

int main()
{   

    std::map<string, string > map_test;   

    map_test.insert(STRING2STRING::value_type("2001", "test1"));
    map_test.insert(STRING2STRING::value_type("2005", "test5"));   

    map<string, string>::const_iterator map_conitor = map_test.begin();
    for(; map_conitor!= map_test.end(); map_conitor++)
    {   

        cout<<map_conitor->first<<"  "<<map_conitor->second<<endl;
    }   

    map<int, vector<string> > m_map; 

    vector<string> m_vec;
    m_vec.resize(2);

    m_vec[0] = "aaaaa";
    m_vec[1] = "bbbbb";
    // m_vec.clear();
    // cout<<m_vec[0]<<endl;

    m_map[0] = m_vec;
    if(m_map[0].empty()) { cout<<"not push_back can not assignment"<<endl;}

    cout<<m_map[0][1]<<endl;

    //  push_back 方式
    vector<string> m_vecPush;
    m_vecPush = m_map[0];
    cout<<"assignment m_vecPush:"<<m_vecPush[0] <<endl;

    map<int, vector<string> > m_mapPush;
    m_vecPush.resize(2);

    m_vecPush.push_back("aaaaa");
    m_vecPush.push_back("bbbbb");

    //  m_mapPush[0] = m_vecPush;   与 m_mapPush.insert 等价
    m_mapPush.insert(pair<int,vector<string> >(0,m_vecPush));  

   if(m_mapPush[0].empty()) { cout<<"push_back can not assignment"<<endl;}
    cout<<m_mapPush[0][1]<<endl;

    // m_mapPush[dev_id].empty()  与  m_mapPush.find(dev_id) == m_mapPush.end() 类似
    if(m_mapPush[2].empty()) {
        cout<<"m_mapPush empty way  not exits"<<endl;
    }

    if(m_mapPush.find(1) == m_mapPush.end()) {
        cout<<"m_mapPush find way  not exits"<<endl;
    }

    /////////////////////////// vector 清空元素  ///////////////////////////////
     // 清空元素,不回收空间
    m_vecPush.clear();
    cout<<"clear vector size:"<<m_vecPush.capacity()<<endl;
    // 清空元素并释放空间
    m_vecPush.clear();
    vector<string>().swap(m_vecPush);
    cout<<"swap vector size:"<<m_vecPush.capacity()<<endl;

 }

之前在开发板上使用

m_vec[0] = "aaaaa"; 这种方式,然后用 m_map[0] = m_vec; 发现m_vec赋值不成功。要用m_vec.push_back("aaaa")这种方式才能赋值给map。 但在台式linux上不存在这种情况。

时间: 2024-12-21 08:31:38

C++ vector 使用笔记的相关文章

vector 学习笔记

vector 使用练习: /**************************************** * File Name: vector.cpp * Author: sky0917 * Created Time: 2014年04月27日 11:07:33 ****************************************/ #include <iostream> #include <vector> using namespace std; int main

Fisher Vector学习笔记

1,背景 现有的模式分类方法主要分为两类,一类是生成式方法,比如GMM,这类方法主要反映同类数据之间的相似度:一类是判别式方法,比如SVM,主要是反映异类数据之间的差异.fisher kernel是想要结合二者的优势(1,生成式方法可以处理长度不一的输入数据,2,判别式方法不能处理长度不一的数据但是分类效果较好.),将生成式模型用于判别式分类器中. 关于处理长度不一的数据,举例说明如下: 我们要对一个图片集I=X1,X2...中的图片做分类,考虑生成式的方法,GMM,是对每一幅图片Xi=x1,.

&lt;&lt;Vector Calculus&gt;&gt;笔记

<<Vector Calculus>>by Paul C, Matthews P4 Since the quantity of |b|*cosθ represents the component of the vector b in thedirection of the vector a, the scalar a * b can be thought of as the magnitudeof a multiplied by the component of b in the

Fisher Vector的改进

<Fisher vector学习笔记>中介绍了fisher vector相关知识,本文接着这片学习笔记,来记录论文<Improving the Fisher Kernel for Large-Scale Image Classification>中第三部分提出的对fisher vector的3种改进. 1,L2 Normalization 首先假设一幅图像的特征们X=xt,t=1...T服从一个分布p,对于Large-Scale image,根据大数定律,样本数T增大时,样本均值收

[笔记]A Practical Guide to Support Vector Classi cation

<A Practical Guide to Support Vector Classication>是一篇libSVM使用入门教程以及一些实用技巧. 1. Basic Kernels: (1)linear (2)polynomial (3)radial basis function (4)sigmoid 2. Scaling: Scaling对于SVM非常重要,可以避免某个维度上的值很大,会主导那些值很小的维度.另一个好处是避免复杂的数值计算.另外需要注意的是,在对training data和

C++学习笔记之由文本文件读取数据到vector模板建立的二维数组 并存储为新的文本文件

阅读本文可首先参考: C++学习笔记之输入.输出和文件 测试数据: 1 /*读取txt文件到二维数组*/ 2 #include <iostream> 3 #include <fstream> 4 #include <vector> 5 #include <string> 6 7 using namespace std; 8 9 typedef vector< vector<int> > D2array; //二维数组 10 typed

C++学习笔记:Vector容器

vector v:初始化一个0大小的向量vector v(10):初始化一个10个大小的向量push_back:增加一个元素 pop:删除一个元素,不返回 front:返回第一个元素 back:返回最后一个元素 at:返回特定位置的元素 capacity:vector的容量,会自动扩大  也可以直接通过v[x]操作元素 /*: Test.cpp */ #include <iostream> #include <vector> #include <string> #inc

[机器学习] Coursera笔记 - Support Vector Machines

序言 机器学习栏目记录我在学习Machine Learning过程的一些心得笔记,包括在线课程或Tutorial的学习笔记,论文资料的阅读笔记,算法代码的调试心得,前沿理论的思考等等,针对不同的内容会开设不同的专栏系列. 机器学习是一个令人激动令人着迷的研究领域,既有美妙的理论公式,又有实用的工程技术,在不断学习和应用机器学习算法的过程中,我愈发的被这个领域所吸引,只恨自己没有早点接触到这个神奇伟大的领域!不过我也觉得自己非常幸运,生活在这个机器学习技术发展如火如荼的时代,并且做着与之相关的工作

STL标准容器类学习笔记之(Vector/Deque/List)

STL标准容器类简介 1.顺序性容器 vector相当与数组,从后面快速的插入与删除,直接访问任何元素 deque双队列,从前面或后面快速的插入与删除,直接访问任何元素 list双链表,从任何地方快速插入与删除 2.关联容器 set快速查找,不允许重复值 multiset快速查找,允许重复值 map一对一映射,基于关键字快速查找,不允许重复值 multimap一对多映射,基于关键字快速查找,允许重复值 3.容器适配器 stack后进先出 queue先进先出 priority_queue最高优先级