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 ...
随机推荐
- 类似a:hover的伪类的注解
a:link { font-size: 14pt; text-decoration: underline; color: blue; } /*设置a对象在未被访问前的样式表属性 .*/ a:hover ...
- C#中的委托和事件2-2(转)
引言 如果你看过了 C#中的委托和事件2-1 一文,我想你对委托和事件已经有了一个基本的认识.但那些远不是委托和事件的全部内容,还有很多的地方没有涉及.本文将讨论委托和事件一些更为细节的问题,包括一些 ...
- laravel中StartSession中间件的问题
今天使用了laravel的dingoapi插件做了一些功能,但是最后遇到一个问题,我在页面和api的路由组中都加了一个相同的以session为基础的身份验证中间件,然后我以管理员身份登录页面时通过了验 ...
- easyui 1.3.3 中combotree post传参问题
重写Tree的loader,增加queryParams属性支持,并且增加setQueryParams方法 //重写tree的loader $.extend($.fn.tree.defaults, { ...
- 转:基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴等)【模式识别中的翘楚】
文章来自于:http://blog.renren.com/share/246648717/8171467499 基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴 ...
- Go Cookie 练习
package main import ( "io" "log" "net/http" ) func main() { http.Handl ...
- AD:想两VIA在同一plane层不同连接(两VIA接同网络),一全连接、一花孔接,实现方法
可以用room方法处理!
- 【HDOJ】4504 威威猫系列故事——篮球梦
水题. #include <cstdio> #include <cstdlib> #include <cstring> #define MAXN 25 ]; voi ...
- codeforces257 div2 D最短路条数
题意: 给一个无向图,总共有 n个点,m+k条边,给定点所连的k条边可以选择删除 问最多删除多少条可以保持该定点到其他点的最短路不变 题解: 从定点出发做单元最短路 首先如果定点到某个点的最短路小于 ...
- 大型分布式C++框架《二:大包处理过程》
本来这一篇是打算写包头在分布式平台中的具体变换过程的.其实文章已经写好了.但是想了这个应该是不能随便发表的.毕竟如果知道了一个包的具体每个字节的意义.能伪造包来攻击系统.其次来介绍一个包的具体变换过程 ...