编程题常用知识点的review。

most important:  想好(1)详尽步骤(2)边界特例,再开始写代码。

I.vector

#include <iostream>
//0.头文件。 特性: 连续存储,动态双倍分配增长
#include <vector>
#include <algorithm> //relevant
using namespace std; bool comp(int a,int b){
return a>b;
} int main(){ //1.创建与初始化
vector<int> v1(,);
vector<int> v2(v1); //也可拷贝初始化 vector<int> v2=v1;
vector<vector<int>> ivec(,v1); //二维vector //2.返回数量与判断空
if(v1.empty())
cout<<"it's empty";
auto n=v1.size(); //3.修改元素与遍历元素
for(int i=;i<n;i++)
v1[i]=; vector<int>::iterator it; //迭代器
for(it=v1.begin();it!=v1.end();it++)
*it=; //4.插入与删除元素
v1.push_back();
v1.pop_back(); v1.insert(v1.begin()+, ); //把9插入到第3个元素后面
v1.erase(v1.begin()+); //把第3个元素后面的那个元素删掉 //5.相关函数 reverse sort
reverse(v1.begin(),v1.end());
sort(v1.begin(),v1.end(),comp); //等价于 sort(v1.rbegin(),v1.rend()); }

II.linked list

#include <iostream>
#include <vector>
//0.struct struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {} //定义了节点的初始化方法
}; using namespace std; int main(){ vector<int> ivec{,,,,,};
//1.创建与初始化
ListNode *head=new ListNode(); //申请一个新节点,注意 head= new ListNode(0)与 head= ListNode(0)的区别
ListNode *p=head,*q;
for(auto i:ivec){ //此乃尾插法,还有头插法,根据情况选择合适的,注意现在有头节点
q=new ListNode(i);
p->next=q;
p=q;
} //2.遍历元素、插入删除元素
p=head;
while(p->next!=NULL){
if(p->next->val==){
q=new ListNode();
q->next=p->next->next;
p->next=q;
}
} /* 3.经典操作: (1) 原地reverse——头插法和尾插法的运用;
(2) 判断cycle——快慢指针;
(3) 找中间节点、找间隔节点——快慢指针;
}
*/ }

III.stack && queue

#include <iostream>
#include <vector> //0.头文件 经常以vector模拟
#include <stack>
#include <queue> using namespace std; int main(){ //1.stack 创建与初始化,基本操作 push pop top empty size
stack<int> myStack;
myStack.push();
myStack.push();
cout<<myStack.top()<<endl;
myStack.pop();
cout<<myStack.size()<<myStack.empty()<<endl; //2.queue 创建与初始化,基本操作 push pop front back empty size
queue<int> myQueue;
myQueue.push();
myQueue.push();
cout<<myQueue.front()<<endl;
cout<<myQueue.back()<<endl;
myQueue.pop();
cout<<myQueue.size()<<myQueue.empty()<<endl; }

IV.map

#include <iostream>
//0.头文件 优点:快速访问/空间换时间/桶的思想,key用来去重,value用来存辅助信息。注意,有时直接用数组进行hash,而不需要借助<map>
#include <map>
using namespace std; int main(){ //1.创建
map<string,int> myMap; //2.访问、修改、增加元素
myMap["Tom"]=; myMap["Lucy"]=; myMap["Jack"]=; //3.删除元素
myMap.erase("Tom"); //也可使用 myMap.erase(myMap.find("hi")); //4.通过key查找元素
if (myMap.find("Tom")==myMap.end())
cout<<myMap.count("Tom")<<endl; //5.遍历,map内部以key的升序排列
for(auto it=myMap.begin();it!=myMap.end();it++){
cout<<it->first<<endl;
cout<<it->second<<endl;
} }

V.string

#include <iostream>
#include <vector>
//0.头文件
#include <string>
#include<sstream> //辅助 using namespace std; int main(){ //1.创建,初始化
string x="hello world",y="ld s";
getline(cin,y); //获得一行,也可再加个终止符参数 //2.常用函数与vector相同 empty, size, push_back, 下标访问 //3.+ - < > =运算符的重载 //4.关于字符变量函数 isalnum,isalpha,isdigit tolower,toupper //5.获得子串
string sub=x.substr(,); // 第1个位置开始,长度为3的子串 //6.交换两个字符串
y.swap(x); //7.查找字串
auto t=y.find(x,);
if(t!=string::npos)
cout<<t; //8.split功能
istringstream strcin(x);
string s;
vector<string> res;
while(strcin >> s)
res.push_back(s); }

VI.bit manipulation

  1. 与                          a&b          1010&1100=1000   
  2. 或                      a|b         1010|1100=1110
  3. 异或                              a^b          1010^1100=1001
  4. 按位取反                         ~a            ~1010=0101
  5. 左移                              a<<1      1010<<1=10100
  6. 带符号右移                     a>>1        -1010>>1=-101
  7. 无符号右移                     a>>>1
 
 

programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation的更多相关文章

  1. STL中stack/queue/map以及Boost unordered_map 的使用方法

    一.stackstack 模板类的定义在<stack>头文件中.stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要的,在不指定容器类型时,默认的容器类型 ...

  2. java三篇博客转载 详解-vector,stack,queue,deque

    博客一:转载自http://shmilyaw-hotmail-com.iteye.com/blog/1825171 java stack的详细实现分析 简介 我们最常用的数据结构之一大概就是stack ...

  3. STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map

      list vector deque stack queue priority_queue set [unordered_set] map [unordered_map] multimap [uno ...

  4. STL容器适配器 stack, queue

    stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...

  5. Stack&&Queue

    特殊的容器:容器适配器 stack     queue     priority_queue:vector+堆算法---->优先级队列 stack:     1.栈的概念:特殊的线性结构,只允许 ...

  6. 数据结构设计 Stack Queue

    之前在简书上初步总结过几个有关栈和队列的数据结构设计的题目.http://www.jianshu.com/p/d43f93661631 1.线性数据结构 Array Stack Queue Hash ...

  7. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  8. PAT 1039 Course List for Student (25分) 使用map<string, vector<int>>

    题目 Zhejiang University has 40000 students and provides 2500 courses. Now given the student name list ...

  9. js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process

    js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process ...

随机推荐

  1. hdu 4530(数学)

    小Q系列故事——大笨钟 Time Limit: 600/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total S ...

  2. 转载自——Json.net动态序列化以及对时间格式的处理

    关于我工作中对Json处理的东西 第一:动态序列化类 第二:时间格式处理 通常我们一个类里 可能有十到更多的属性,但是我们序列化通常只需要序列化其中的 三到五个这样的话就会有多余的数据 如果 我只想序 ...

  3. ios开发某个页面横不过来屏幕的时候

    某一个页面需要横屏,其他的页面任然保持竖屏需要以下关键的几个步骤: 1.修改系统代理方法的返回值 -(UIInterfaceOrientationMask)application:(UIApplica ...

  4. 爬虫学习笔记(二)http请求详解

    上篇博客里面写了,爬虫就是发http请求(浏览器里面打开发送的都是http请求),然后获取到response,咱们再从response里面找到想要的数据,存储到本地. 咱们本章就来说一下什么是http ...

  5. Codeforces 586D Phillip and Trains(DP)

    题目链接 Phillip and Trains 考虑相对位移. 每一轮人向右移动一格,再在竖直方向上移动0~1格,列车再向左移动两格. 这个过程相当于每一轮人向右移动一格,再在竖直方向上移动0~1格, ...

  6. Codeforces 509E(思维)

                                                                                                         ...

  7. Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  8. noip2017集训测试赛(三)Problem C: MST

    题面 Description 给定一个n个点m条边的连通图,保证没有自环和重边.对于每条边求出,在其他边权值不变的情况下,它能取的最大权值,使得这条边在连通图的所有最小生成树上.假如最大权值为无限大, ...

  9. 使用aspnet_regsql.exe 创建ASPState数据库,用来保存session会话

    使用aspnet_regsql.exe 创建ASPState数据库,用来保存session会话   因为公司有多台服务器,所以session要保存在sql server上,因此要在数据库中建立存放se ...

  10. 性能调优培训 windbg --woodytu

    http://www.cnblogs.com/woodytu/p/4675479.html http://www.cnblogs.com/fjicn/p/3405716.html http://www ...