标签:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <string>
using namespace std;
/**
* 初始化队列
*/
void init_queue()
{
vector<int> ivec;
list<int> ilist(ivec.begin(), ivec.end());
deque<int> ideque(ivec.begin(), ivec.begin() + ivec.size() / 2);
char *words[] = { "o", "s", "o", "f", "o" };
size_t words_size = sizeof(words) / sizeof(char *);
vector<string> svec(words, words + words_size);
list<string> slist(64," sda");
cout << slist.size() << endl;
}
/**
* 正向迭代
*/
void iterator_queue(vector<string> svec)
{
/*for (vector<string>::iterator iter = svec.begin(); iter!=svec.end();
iter++)
{
cout << *iter << endl;
}*/
vector<string>::iterator iter = svec.begin();
while (iter!=svec.end())
{
cout << *iter << endl;
iter++;
}
}
/**
* 反向迭代
*/
void reverse_iterator_queue(vector<string> svec)
{
for (vector<string>::reverse_iterator iter = svec.rbegin(); iter!
=svec.rend();iter++)
{
cout << *iter << endl;
}
}
/**
* 添加参数
* push_back 所有队列
* push_front 除vector以外
* insert 插入到指定迭代器前一个位置
*/
vector<string> add_element_2_queue()
{
char *words[] = { "o", "s", "o", "f", "o" };
size_t words_size = sizeof(words) / sizeof(char *);
vector<string> svec(words, words + words_size);
svec.push_back("sdzf");
svec.insert(svec.end() - 1, "asda");
return svec;
//list<string> slist(words, words + words_size);
//slist.push_front("dfg");
//slist.insert(slist.begin + 1, svec.begin, svec.begin+svec.size() /
2);
}
/**
* 另一种获取获取元素的方式
*/
void get_element_from_queue()
{
vector<string> svec = add_element_2_queue();
////获取第一个
//svec.front();
//*svec.begin();
////获取最后一个
//svec.back();
//*svec.end();
////取某个
//svec[3];
//svec.at(3);
vector<string>::size_type svec_size = svec.size();
int index = 0;
while (index!=svec_size)
{
cout << svec.at(index++) << "\n";
}
}
/**
* 抹除元素
*/
vector<string> remove_element_2_queue()
{
vector<string> svec = add_element_2_queue();
while (!svec.empty())
{
svec.pop_back();
}
//svec.erase(svec.begin());
//svec.erase(svec.begin(), svec.begin() + svec.size() / 2);
//svec.pop_back();
//svec.clear();
return svec;
}
/**
* 定义两个数值不等的队列 并对其交换
* 虽然做了交换 但交换前迭代器指向的是元素的地址 在交换后并不会改变
*/
void swap_queue()
{
char *words[] = { "o", "s", "o", "f", "o" };
char *words2[] = { "k", "u", "y", "g", "r","t" };
size_t words_size = sizeof(words) / sizeof(char *);
vector<string> svec(words, words + words_size);
vector<string>::iterator iter = svec.begin();
vector<string> svec2(words2, words2 + words_size+1);
//svec.assign(svec2.begin(), svec2.end()-2);
svec.swap(svec2);//无论数量是否匹配 都是全部交互
//iterator_queue(svec);
}
/**
* reserve()函数声明预留空间
* 队列个数超过容量值 并且队列所处的内存片段不够用 则重新分配内存地址
* 队列个数超过容量值 并且队列所处的内存片段够用 则扩容
*/
void init_capacity()
{
char *words[] = { "k", "u", "y", "g", "r", "t" };
size_t words_size = sizeof(words) / sizeof(char *);
vector<string> svec(words, words + words_size);
//svec.reserve(20);
cout << svec.size() << " "<< svec.capacity() << endl;
cout << &svec << endl;
svec.push_back("NEW DATA");
cout << &svec << endl;
cout << svec.size() << " " << svec.capacity() << endl;
svec.push_back("NEW DATA");
svec.push_back("NEW DATA");
svec.push_back("NEW DATA");
cout << &svec << endl;
cout << svec.size() << " " << svec.capacity() << endl;
}
#include <iostream>
#include <stack>
#include <queue>//队列和优先级队列
#include <vector>
using namespace std;
/**
* stack:all
* queue:push_front()--->list deque;
* priority_queue:随机访问 vector deque;
*/
void init_container()
{
deque<int> deq;
stack<int> istk(deq);
vector<int> ivec;
stack< int, vector<int> > istk2(ivec);
}
/**
* stack的用法
*/
void stack_mth()
{
stack<int>::size_type stack_size = 10;
stack<int> istk;
size_t index = 0;
while (index++ != stack_size)
{
istk.push(index);
}
while (!istk.empty())
{
cout << istk.top() << endl;
istk.pop();
}
}
/**
* queue的用法,不支持top()
*/
void queue_mth()
{
queue<int>::size_type queue_size = 20;
queue<int> iqueue;
int index = 0;
while (index++ != queue_size)
{
iqueue.push(index);
}
while (!iqueue.empty())
{
cout << iqueue.front() << endl;
iqueue.pop();
}
}
void pri_que_mth()
{
priority_queue<int>::size_type pri_queue_size = 15;
priority_queue<int> ique;
int index = 0;
while (index++ != pri_queue_size)
{
ique.push(index);
}
while (!ique.empty())
{
cout << ique.top() << endl;
ique.pop();
}
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <utility>//pair的声明头文件
#include <map>
#include <set>
#include <vector>
#include <stdio.h>
using namespace std;
typedef map<int, string> _StudMap;
typedef _StudMap::iterator _StdMapIter;
typedef _StudMap::value_type _SingleStdPair;
typedef _StudMap::key_type _SingleStdKeyTpy;
typedef _StudMap::mapped_type _SingleStdValTpy;
/**
* pair的操作
*/
void pair_mth()
{
typedef pair <int, int> _IntPair;
_IntPair p1(5, 6);
pair <int, const char *> p2 = make_pair(6, "sadsa");
if (p2.first==6&&p2.second=="sadsa")
{
cout << "equals";
}
}
/**
* 定义Map的三种形式
*/
_StudMap init_mapContainer()
{
//_StudMap myMap;
//_StudMap myMap2(myMap);
size_t map_size = 10;
vector< _SingleStdPair > ivec;
int index = 0;
while (index++ != map_size)
{
char res[20];
sprintf(res, "%d---", index);
ivec.push_back(make_pair(index, res));
}
_StudMap myMap3(ivec.begin(), ivec.begin() + ivec.size() / 2);
return myMap3;
}
/**
* value_type >>>>>>pair
* key_type
* mapped_type
*/
void map_type()
{
_StudMap map = init_mapContainer();
;
_StudMap::size_type map_size= map.size();
for (_StdMapIter iter = map.begin(); iter != map.end();iter++)
{
_SingleStdPair pair = *iter;
_SingleStdKeyTpy key = pair.first;
_SingleStdValTpy val = pair.second;
cout << key << " " << val << endl;
}
}
void map_add_find()
{
//通过下标访问元素
//如果没有对应的值就创建一个键值对并赋初始值
map<string, int> word_count;
word_count["HELLO"] = 51;
//cout << word_count["HELLO"];
//word_count["HELLO"]=55;
//cout << word_count["HELLO"];
//insert()
//word_count.insert(map<string, int>::value_type("ANNA",1));
//word_count.insert(make_pair("ANNAY",2));
//find() count()
map<string, int>::iterator iter = word_count.find("HELLO");
if (iter != word_count.end())
{
cout << iter->second << endl;
}
cout << word_count.count("HELLO");
}
void map_delete()
{
_StudMap map = init_mapContainer();
//int remove_key=map.erase(2);
//cout << "remove count :" << remove_key;
_StdMapIter iter= map.erase(map.begin());
cout << "after iter_key:" <<(*iter).first;
}
void map_iter()
{
_StudMap map = init_mapContainer();
_StdMapIter iter = map.begin();
while (iter != map.end())
{
cout << iter->second;
iter++;
}
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <utility>
#include <set>
#include <vector>
using namespace std;
set<int> init_set()
{
set<int> iset;
for (size_t i = 0; i < 10; i++)
{
iset.insert(i);
iset.insert(i);
}
return iset;
}
//map element is read-Only
void read_set()
{
set<int> iset = init_set();
//cout << iset.size();
set<int>::iterator iter = iset.find(3);
if (iter != iset.end())
{
cout << *iter;
}
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <vector>
using namespace std;
typedef multimap<string, string> _MultiMap;
typedef _MultiMap::size_type _MultiMapSize;
typedef _MultiMap::iterator _MultiMapIter;
typedef pair<_MultiMapIter, _MultiMapIter> _EntryPair;
_MultiMap init_map()
{
_MultiMap mtp;
mtp.insert(make_pair("KEY_A", "AA"));
mtp.insert(make_pair("KEY_A", "BB"));
mtp.insert(make_pair("KEY_A", "CC"));
mtp.insert(make_pair("KEY_B", "CCK"));
mtp.insert(make_pair("KEY_B", "CCD"));
mtp.insert(make_pair("KEY_C", "jt"));
mtp.insert(make_pair("KEY_C", "cfh"));
mtp.insert(make_pair("KEY_C", "cfgd"));
return mtp;
}
_MultiMapSize multimap_remove()
{
_MultiMap mtp = init_map();
return mtp.erase("KEY_A");
}
void multimap_find(){
_MultiMap map = init_map();
string search_item("KEY_A");
_MultiMapSize size = map.count(search_item);
_MultiMapIter iter = map.find(search_item);
if (iter!=map.end())
{
for (size_t index = 0; index != size;index++,iter++)
{
cout << iter->second << endl;
}
}
}
/**
* upper_bound() 某个对象上限
* lower_bound() 某个对象下限
* equal_range() 定义对象范围
*/
void multimap_find_new()
{
_MultiMap map = init_map();
string search_item("KEY_C");
//_MultiMapIter upIter = map.upper_bound(search_item),
// lowerIter = map.lower_bound(search_item);
//while (lowerIter != upIter )
//{
// cout << lowerIter++->second << endl;
//}
_EntryPair pair = map.equal_range(search_item);
while (pair.first!=pair.second)
{
cout << (pair.first)++->second << endl;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/qq285016127/article/details/46833917