【大爱】C++STL 常用 函数 用法 - duoduo3_69 - 博客园
1 vector 要点:
leetcode 88
1 ) 分配 nums3.assign(len1,0);2) 清空 nums1.resize(0);
3) 遍历 4) 交换 nums3.swap(nums1);
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int len1=nums1.size();
int len2=nums2.size();
int num=0,i=0,j=0;
vector<int> nums3;
//分配
nums3.assign(len1,0);
int x=0,y=0,k=0;
while (y<len2&&x<len1-len2) {
if (nums1[x]<=nums2[y])
nums3[k++]=nums1[x++];
else
nums3[k++]=nums2[y++];
}
if (x==(len1-len2)) {
while (y<len2)
nums3[k++]=nums2[y++];
} else if (y==len2) {
while (x<len1-len2)
nums3[k++]=nums1[x++];
}
//清空
nums1.resize(0);
//遍历
for(int z=0;z<len1;z++)
{
cout<<nums3[z]<<" ";
}
//交换
nums3.swap(nums1);
}
push_back,insert,remove
<Pamara *> sara;
Vector<Pamara *>:: iterator it;
For(it=sara.begin();it!=sara.end();it++)
If( (*it)&&(*it)->data==bara->data )
{
Delete(*it);
Sara.erase(it);
}
错在哪里?answer:erase在删除之后,自动+1,这样下一个for循环,又加1. 所以加了2次.skip一个.
正确的解法是,
for(vector<int>::iterator it=arr.begin(); it!=arr.end(); )
{
if(* it == 8)
{
it = arr.erase(it);
}
else
{
++it;
}
}
详见:http://blog.csdn.net/wangwenwen/article/details/7583986
1.2 erase and remove different?
https://blog.csdn.net/ozwarld/article/details/7761519
1.3 vector 释放内存
简单的程序诠释C++ STL算法系列之十五:swap
std::vector(X).swap(X)
作用相当于:
{
std::vector temp(X);
temp.swap(X);
}
二维vector:C++二维vector使用教程_clarkjs的博客-CSDN博客_二维vector
2 map&piar&hashmap
Map 使用实例
//1 map 定义
//2 map 清空
//3 map 赋值
//4 map 遍历
int lengthOfLongestSubstring(string s) {
//1 map 定义
map<char,int> m;
int len= s.length();
int sum=0,k=0;
if (s=="") return 0;
if (s.length()==1) return 1;
for (int j=0;j<len;j++) {
k=1;
//2 map 清空
m.clear();
//3 map 赋值
m[s[j]]=1;
for (int i=j+1;i<len;i++) {
char temp= s[i];
if (m[temp]==1) {
break;
} else {
k++;
m[temp]=1;
}
}
if (k>sum) sum=k;
//4 map 遍历
for(map<char,int>::iterator iter = m.begin(); iter != m.end(); ++iter){
cout<<"key:"<<iter->first<<" value:"<<iter->second<<endl;
}
}
return sum;
1) map和pair区别
map是stl,有iterator
pair是struct,里面有两个item。
map的value可以是pair。map不能做value。pair 不能iterator
2)
name lib 实现 优缺点 用法一样
map stl map用红黑树 慢。占用空间小. insert,find,erase etc
hash_map boost. c11 hash
快。占用空间大
例如:除了n作为散列值,也要新增加n个散列值的空间.
insert,find,erase etc
例如://18959 map insert 有的callid 插入到前面
m_proxysContainer.insert(pair >(id, proxy)).second);
map automatic sort by key, 后insert的可能先被find到;18959 map insert 有的callid 插入到前面
map the third parameter , write func to control sort
C++ STL中Map的按Key排序和按Value排序 http://blog.csdn.net/iicy266/article/details/11906189
这个blog中函数对象,即是用c++ struct 代替class ,实现compare的封装。
map面试题: what is map insert return value; the reuturn value is ret, ret type is pair , pair's the second value is bool
map也是使用红黑树,他是一个键值对(key:value映射),便利时依然默认按照key程序的方式遍历,同set。
deque
deque和vector一样,采用线性表,与vector唯一不同的是,deque采用的分块的线性存储结构,每块大小一般为512字节,称为一个deque块,所有的deque块使用一个Map块进行管理,每个map数据项记录各个deque块的首地址,这样以来,deque块在头部和尾部都可已插入和删除元素,而不需要移动其它元素。使用push_back()方法在尾部插入元素,使用push_front()方法在首部插入元素,使用insert()方法在中间插入元素。一般来说,当考虑容器元素的内存分配策略和操作的性能时,deque相对vectore更有优势。(下面这个图,我感觉Map块就是一个list< map >)
list
插入:push_back尾部,push_front头部,insert方法前往迭代器位置处插入元素,链表自动扩张,迭代器只能使用++--操作,不能用+n -n,因为元素不是物理相连的。
遍历:iterator和reverse_iterator正反遍历
删除:pop_front删除链表首元素;pop_back()删除链表尾部元素;erase(迭代器)删除迭代器位置的元素,注意只能使用++--到达想删除的位置;remove(key) 删除链表中所有key的元素,clear()清空链表。
查找:it = find(l.begin(),l.end(),key)
排序:l.sort()
删除连续重复元素:l.unique() 【2 8 1 1 1 5 1】 --> 【 2 8 1 5 1】
bitset
从来没用过,上两幅图吧就:
stack(后进先出)
这个印象深刻,学数据结构的时候做表达式求值的就是用的栈。
queue(先进先出)
queue有入队push(插入)、出队pop(删除)、读取队首元素front、读取队尾元素back、empty,size这几种方法