第一课:C++ STL 2020.02.10

第一课:C++ STL 2020.02.10

1.Hello World!

#include <iostream>
using namespace std;
int main()
{
    cout<<"Hello"<<endl;
    system("pause");
    return 0;
}
using namespace std;

C++用标准库的东西前面要加std::
加了using namespace std;就可以不写std::
但是prev等等就会冲突

cout<<"Hello"<<endl;

所有标准数据类型都可以用cout输出;
endl可以清空缓存区->交互题

bool 类型;cppstring-> #include<string>;
cin.get() = getchar()
cin.getline(cString,10000) = gets(cString) 要加读入的字符数上限
or
getline(cin,line)

cin判断EOF
while(cin>>a)
{

}

CautionXD

cin慢 1e5以上用cin会TLE 建议用scanf
输出小数用printf更方便一点 C++控制格式输出需要<iomanip>
->cout.setprecistion(int digit)

部分特性

动态内存

int* number = new int[100];
delete

引用

int& a = number;
Ex: swap f
void swapi(int& a,int& a)
{
int c = a;
a = b;
b = c;
}

函数重载

允许创建同名但是参数列表不同的函数
int dfs(int a = 1)
{
}
dfs();

struct构造函数

No typedef struct required;
构造函数->初始化结构
struct node
{
int a;
node(int _a = 0)//同名
{
a = _a;
}
}

<vector>

vector<int> arr1(100);
vector<int> list;
list.push_back(1);

pointer -> iterator
vector::iterator p1;
for(p1 = arr1.begin(); p1 !=arr1.end();p1++)
arr1.end()->最后一个的下一个

list.size()数组元素个数O(1)
list.clear()清空数组O(n)
list.empty()数组是否为空O(1)
list.begin()数组首元素迭代器O(1)
list.end()数组最后一个元素的下一个元素的迭代器O(1)
list.erase(p1)删除数组某个迭代器所在位置的数字O(n)
list.push_back()添加元素O(1)
list.pop_back()删除最后一个元素O(1)

<string>

str.length();str.size(); O(n)
str.insert(1,“aaa”);O(n)
str.insert(iterator,‘a’);O(n)
str.c_str()返回C语言字符串 用于printfO(n)
str.append(str2)O(n)
str.compare(str2)
str == str2;
str+=str2;
str+=‘a’;

<algorithm>

int arr[N];
sort(arr,arr+N)最后一个元素的下一个元素指针
O(nlogn)
vector<int>arr
sort(arr.begin(),arr.end())

bool cmp(int a,int b)
{
return a>b;//降序
}
sort(arr,arr+N,cmp)

min() max()
min_element(arr.begin(),arr.end()); max_element
nth_element(arr.begin(),arr.begin()+n,arr.end());

swap(a[0],a[1])
reverse(arr.begin(),arr.end())
int newLength = unique(arr.begin(),arr.end())-arr.begin();
排好序了才能用unique

二分查找
bool isExist = binary_search(arr.begin(),arr.end(),1)

int firstloc = lower_bound(arr.begin(),arr.end(),2)-arr.begin();第一个比查找值大/小的数据
int lastloc = upper_bound(arr.begin(),arr.end(),2)-arr.begin();

猜你喜欢

转载自blog.csdn.net/Leo9344/article/details/104243845