总结一下STL容器的用法,之前笔记都记在笔记本上了。一次回家,笔记本放家里了,没带。搞得好多关于STL的知识点都忘了。为此以后所有笔记都搬到博客上。为了以后自己用能翻到笔记。
第一节:vector的笔记
使用一下vector,这个可是是一个动态数组。可以根据你的需求而扩展。
用法:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> V;//V是一个空的容器
std::vector<double> W(10,2);//W是一个容器,里面有10个2;
std::vector<double> WW(W);//把W中的数据拷贝到WW中
V.push_back(1); //向V中添加一个1
V.push_back(2);
W.push_back(3);
W.push_back(4);
V.pop_back();//删除最后一个数据
std::cout << V.size() << std::endl;
std::cout << W.size() << std::endl;
//vector也可以像数组一样操作比如
W[2] = 10;
//动态循环给V添加数据
int k;
for (int i = 0; i < 3; ++i)
{
std::cin >> k;
V.push_back(k);
}
for (auto x : V)//c++11以后支持的循环格式
{
std::cout << x << " ";
}
std::cout << std::endl;
for (auto x : WW)
{
std::cout << x << " ";
}
std::cout << std::endl;
for (std::vector<double>::size_type i = 0; i != W.size(); ++i)//传统vector遍历输出
{
std::cout << W[i] << " ";
}
//使用vector动态申请二维数组
for (int q = 5; q > 0; q--)
{
//std::cin >> m >> n;
std::vector<std::vector<int> > vecInt(q+1, std::vector<int>(q+2));
for (int i = 0; i < q+1; ++i)
{
for (int j = 0; j < q+2; ++j)
{
vecInt[i][j] = i + j;
}
}
for (int i = 0; i < q+1; ++i)
{
for (int j = 0; j < q+2; ++j)
{
std::cout << "vecInt[" << i << "][" << j << "] = " << vecInt[i][j] << " \t";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
return 0;
}
#include <iostream>
#include <vector>
int main()
{
std::vector<int> Vint(10,0);
auto it = Vint.begin();
it+=4; //在脚标为4的位置插入数据,从0开始数
Vint.insert(it,9); //在指定位置插入一个数
//Vint.assign(2,8); //替换,2个8替换成Vint原来的数据,最后打印的数据只有 8 8
Vint[2] = 8; //2号脚标替换数据为8
std::cout << Vint.size() << std::endl;//Vint长度为11
for(auto i =0; i < Vint.size(); ++i)
{
std::cout << Vint[i] << " ";
}
std::cout << std::endl;
return 0;
}
用vector动态申请二维数组,跨函数使用,在C++类中的应用
vec.h头文件
#ifndef __VEC_H__
#define __VEC_H__
#include <iostream>
#include <vector>
#include <algorithm>
typedef struct DW //定义一个二维数组,为了跨函数使用,定义一个数据类型
{
std::vector<std::vector<int> > vecInt;//二维数组
}dw,*pDW;
class DWT//一个类
{
public:
void vec(pDW);//打印数组函数
void init();//初始化数组函数
pDW getVec() { return pdw; };//获取自定义数组类型(二维数组)
private:
pDW pdw;//声明自定义数组
};
#endif // !__VEC_H__
main函数
#include "vec.h"
int main()
{
//使用vector动态申请二维数组
//std::cin >> m >> n;
DWT dwt;//初始化类
dwt.init();//初始化数组
dwt.vec(dwt.getVec());//打印数组
return 0;
}
init.cpp
#include "vec.h"
void DWT::init()
{
int n , m;
std::cin >> n >> m;
pdw = new dw;//申请自定义数据类型内存
pdw -> vecInt = std::vector<std::vector<int> >(n, std::vector<int>(m));//初始化vector二维数组
//c参考https://www.cnblogs.com/feifanrensheng/p/8711601.html
for (std::vector<int>::size_type i = 0; i != pdw->vecInt.size(); ++i)//赋值操作
{
for (std::vector<int>::size_type j = 0; j != pdw->vecInt[i].size(); ++j)
{
pdw->vecInt[i][j] = i + j;
}
}
}
vec.cpp
#include <iostream>
#include <vector>
#include "vec.h"
void DWT::vec(pDW)//打印函数
{
for (std::vector<int>::size_type i = 0;i != pdw->vecInt.size(); ++i)
{
for (std::vector<int>::size_type j= 0;j != pdw->vecInt.size(); ++j)
{
std::cout << "vecInt[" << i << "][" << j << "] = " << pdw -> vecInt[i][j] << " \t";
}
std::cout << "vecInt[i]的长度是" << pdw->vecInt[i].size() << " " << "vecInt的长度是" << pdw->vecInt.size();
std::cout << std::endl;
}
std::cout << std::endl;
}
这样一个跨函数使用二维数组就实现了。