C++ 基本数据结构整理
Hash Map (Unordered_map)
- Insert
#include <unordered_map>
using namespace std; unordered_map <char, bool> hash_map;
hash_map.insert(make_pair<char,bool>('a',true)); - find
if(hash_map.find(num) == hash_map.end())
cout << "not found" << endl;
unordered_map <char, bool> :: iterator got = hash_map.find('a');
got->first 指向key
got->second 指向value
String
- 如何遍历string里每个元素
//sa is a string
for( string::iterator p = sa.begin() ; p!= sa.end() ; p++ )
cout << *p ;
cout << endl; 如何使用string里的find
string:size_type pos = str.find(''); // pos is the position in string you find. if(pos==string::npos)
cout << "cannt find";
else
cout << pos;在string内的特定位置插入元素
// inserting into a string
#include <iostream>
#include <string> int main ()
{
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it; // used in the same order as described above:
str.insert(,str2); // to be (the )question
str.insert(,str3,,); // to be (not )the question
str.insert(,"that is cool",); // to be not (that is )the question
str.insert(,"to be "); // to be not (to be )that is the question
str.insert(,,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+,','); // to be(,) not to be: that is the question
str.insert (str.end(),,'.'); // to be, not to be: that is the question(...)
str.insert (it+,str3.begin(),str3.begin()+); // (or ) std::cout << str << '\n';
return ;
}- Substr: 截取string的一部分
// string::substr
#include <iostream>
#include <string> int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead) std::string str2 = str.substr (,); // "generalities" std::size_t pos = str.find("live"); // position of "live" in str std::string str3 = str.substr (pos); // get from "live" to the end std::cout << str2 << ' ' << str3 << '\n'; return ;
}
Type Conversion
- Char -> Int
char s = '5'
int a = s-'0'; - Int -> String
string s = std::to_string();
- String -> Int
std::string myString = "";
int value = atoi(myString.c_str()); //value = 45 You cann't do switch-case statement on string by C++
Stack
- 如何压入一个pair
stack<pair<TreeNode*, int> > s;
s.push(make_pair(root,)); TreeNode *tmp = s.top().first;
int len = s.top().second; 用STL stack中的push(),pop(),top()
// STL 栈适配器(stack)
#include <iostream>
#include <stack>
using namespace std; int main()
{
stack<int> is; //定义栈对象 for (int i = ; i < ; ++i)
is.push(i+); //将100~199一次顺序入栈 cout<<"top element:"<<is.top()<<endl; //查看栈顶元素
is.pop(); //出栈操作
cout<<"new top element:"<<is.top()<<endl; //查看栈顶元素
cout<<"size:"<<is.size()<<endl; //查看栈高度 return ;
}
Heap
- 如何用已有的数据结构实现heap的操作包括push_head, pop_head, sort_heap
// range heap example
#include <iostream> // std::cout
#include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector> // std::vector int main () {
int myints[] = {,,,,};
std::vector<int> v(myints,myints+); std::make_heap (v.begin(),v.end());
std::cout << "initial max heap : " << v.front() << '\n'; std::pop_heap (v.begin(),v.end()); v.pop_back(); //pop_heap()用于弹出堆中的第一个元素,并把它放到区间的最后一个位置,然后重新将前面的元素构建成一个堆。
std::cout << "max heap after pop : " << v.front() << '\n'; v.push_back(); std::push_heap (v.begin(),v.end()); // push_heap()用于将指定区间的最后一个元素加入堆中并使整个区间成为一个新的堆。注意前提是最后一个元素除外的所有元素已经构成一个堆。
std::cout << "max heap after push: " << v.front() << '\n'; std::sort_heap (v.begin(),v.end()); std::cout << "final sorted range :";
for (unsigned i=; i<v.size(); i++)
std::cout << ' ' << v[i]; std::cout << '\n'; return ;
} Output:
initial max heap :
max heap after pop :
max heap after push:
final sorted range :
Array & Vector
- 多维vector如何判断行和列的大小
vector<vector<int> > matrix
int rowSize = matrix.size();
int colSize = matrix[].size() - 使用insert()在一个指定的position前插入一个元素
#include<iostream>
#include<vector>
using namespace std; int main()
{
vector<int> v();
v[]=;
v[]=;
v[]=; //在最前面的元素前插入8
v.insert(v.begin(),); //在第二个元素前插入新元素1
v.insert(v.begin()+,); //在末尾插入新元素1
v.insert(v.end(),); for(vector<int>::iterator it=v.begin();it!=v.end();it++)
cout<<*it<<endl;
system("pause");
return ;
} - 用STL查找vector中最小元素的方法
cout<<"maxinum:"<<*min_element(v.begin(),v.end())<<endl;
int index = min_element(v.begin(),v.end())-v.begin(); - 用vector创建多维数组. 多维的数组被写做 vector<vector<int>空格>. 其实是描述了一个二维数组/矩阵 a。其初始化可以参考下面的代码
std::vector<std::vector<int> > a;
vector< vector<int> > intVV;
vector<int> intV;
int i,j;
for(i=;i<;++i){
intV.clear();
for(j=;j<;++j)
intV.push_back(i*+j);
intVV.push_back(intV); - 一个遍历数组的简单写法
int num[] = {,,};
for(int i:num) // You must define i here.
cout << i << endl; - vector中的pop/push
#include <vector>
using namespace std; vector<char> array;
array.push_back('9'); char num = array.back();
array.pop_back();
STL操作符重载来改变sort算法
// 排序元素,比较的对象
struct Person
{
Person(int id, const string& name, int age): id_(id), name_(name), age_(age)
{} int id_;
string name_;
int age_;
}; // 方式1:重载operator<用于排序时的比较(写在函数体内)
bool operator< (const Person& rt)
{
return this->id_ < rt.id_;
} // 排序函数写法,默认调用operator<
sort(members.begin(), members.end()); // 方式2:写比较函数
bool CompAge(const Person& pl, const Person& pr)
{
return pl.age_ < pr.age_;
} // 排序时传入比较函数指针
sort(members.begin(), members.end(), CompAge); // 方式3:仿函数
struct CompName
{
bool operator()(const Person& pl, const Person& pr)
{
return pl.name_ < pr.name_;
}
}; // 排序时传入函数对象
sort(members.begin(), members.end(), CompName());
Leetcode中数据结构的定义
Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
C++ 基本数据结构整理的更多相关文章
- 转 Python常见数据结构整理
http://www.cnblogs.com/jeffwongishandsome/archive/2012/08/05/2623660.html Python常见数据结构整理 Python中常见的数 ...
- Java数据结构整理(一)
ava数据结构内容整理关键字: 数据结构 Collection:List.SetMap:HashMap.HashTable如何在它们之间选择一.Array , ArraysJava所有“存储及随机访问 ...
- (数据结构整理)NJUPT1054
这一篇博客以一些OJ上的题目为载体,整理一下数据结构.会陆续的更新. .. 我们都知道,数据结构的灵活应用有时能让简化一些题目的解答. 一.栈的应用 1.NJUPT OJ 1054(回文串的推断) 回 ...
- redis数据结构整理(二)
摘要: 1.各个数据结构的应用举例 1.1 String类型应用举例 1.2List类型应用举例 1.3Set类型应用举例 1.4Sorted Set类型应用举例 1.5Hash类型应用举例 内容: ...
- redis数据结构整理(一)
摘要: 0.redis安装 1.redis的常用数据结构类型 1.1 String 1.2 List 1.3 Set 1.4 Sorted Set 1.5 Hash 2.redis是单进程单 ...
- java数据结构整理(二)
一.List接口,有序的Collection接口,能够精确地控制每个元素插入的位置,允许有相同的元素 1.链表,LinkedList实现了List接口,允许null元素,提供了get().remove ...
- Python常见数据结构整理
Python中常见的数据结构可以统称为容器(container).序列(如列表和元组).映射(如字典)以及集合(set)是三类主要的容器. 一.序列(列表.元组和字符串) 序列中的每个元素都有自己的编 ...
- java常见数据结构整理
java中容器类数据结构主要在java.util包中. java.util包中三个重要的接口及特点:List(列表).Set(保证集合中元素唯一).Map(维护多个key-value键值对,保证key ...
- acm数据结构整理
一.区间划分 //区间划分+持久化并查集:区间连通情况统计. inline bool comp(Ask x, Ask y){return x.km == y.km ? x.l > y.l : x ...
随机推荐
- PHP面向对象多态性的应用
多态是面向对象的三大特性中除封装和继承之外的另一重要特性.它展现了动态绑定的功能,也称为“同名异式”.多态的功能可让软件在开发和维护时,达到充分的延伸性.事实上,多态最直接的定义是让具有继承关系的不同 ...
- 求最小生成树(Prim算法)(1075)
Description 求出给定无向带权图的最小生成树.图的定点为字符型,权值为不超过100的整形.在提示中已经给出了部分代码,你只需要完善Prim算法即可. Input 第一行为图的顶点个数n ...
- 成功启动了Apache却没有启动apache服务器
原因没有用管理员身份运行...
- BZOJ 3240: [Noi2013]矩阵游戏
3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1586 Solved: 698[Submit][Status ...
- UIImagePickerController 操作图库
UIImagePickerController详解 转载自:http://blog.csdn.net/kingsley_cxz/article/details/9157093 1.UIImage ...
- pin导致路由器死掉的解决方法
首先检测网卡: ifconfig -a 然后模拟端口: airmon-ng start wlan0 接下来用: airodump-ng mon0 扫描ap找到你pin死的路由器mac 用mdk3 做身 ...
- .net performance
http://msdn.microsoft.com/en-us/library/ms173196.aspx http://www.zhihu.com/question/20314377 http:// ...
- ASP.NET Web.Config 读资料 (学习笔记)
refer : http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 上面这篇写很好了. 在做项目时,我们经常会遇到一些资料,我 ...
- rsync同步目录及同步文件
最简单的只读同步工作. 一,服务端的配置 1,安装rsync(阿里云默认已有此程序) 略 2,生成文件rsyncd.conf,内容如下: #secrets file = /etc/rsyncd.sec ...
- GS1011无线模块的使用简介。
一.硬件说明: 只是用电脑的串口助手与之通信,利用了max232进行电平转换.是用模块的UART0作为通信接口. 模块引脚 接点 说明 1.17.32.48 GND 模块地 9 3.3V VB ...