C++ 记录
1.输入输出切片
#include <iostream>
using namespace std;
int main(void)
{
getline(cin,a);//输入
string s_sub = s.substr(1,2);//切片
}
#include <iostream>
using namespace std;
int main(void)
{
string s1 = "ppx";
float cc = 45.54;
double cc2 = 45.54;
string s2 = "haha";
bool a_bool = true;
int c = 0;
cout<< s1+s2 <<endl;
printf("aaa");
int a = 10;
printf("%d",&a);
printf("\n");
cout<< s2.length();
c =s2.length();
printf("\n");
printf("%d",c);
printf("\n");
cout<<typeid(c).name()<<endl;
cout<<typeid(a).name()<<endl;
cout<<typeid(s1).name()<<endl;
cout<<typeid(s2).name()<<endl;
cout<<typeid(cc).name()<<endl;
cout<<typeid(cc2).name()<<endl;
cout<<typeid(a_bool).name()<<endl;
return 0;
}
ppxhaha
aaa-840036612
4
4
i
i
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
f
d
b
2. 结构体
注意可以直接通过结构体名称定义结构体类型变量的写法
//g++ 7.4.0
#include <iostream>
using namespace std;
struct stu{
string name = "ppx";
int age = 10;
};
int main()
{
stu a;
stu b[10];
cout << a.age << endl;
cout << b[1].age<< endl;
cout << "Hello, world!\n";
}
输出
10
10
Hello, world!
3.引用和传值(和取地址符号没有关系)
详细比较
引用输入5,传入后取地址,对地址上的数据区数据进行修改
#include <iostream>
using namespace std;
void c(int &a)
{
a+=1;
}
int main(void)
{
int a = 4;
c(a);
cout << a;
}
引用输入4,传入后取数据区,不对地址上的数据区数据进行修改
//g++ 7.4.0
#include <iostream>
using namespace std;
void c(int a)
{
a+=1;
}
int main(void)
{
int a = 4;
c(a);
cout << a;
}
4.数组
//g++ 7.4.0
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> v(10,2);
for(int i=0; i<10;i++)
cout<<v[i]<<endl;
std::cout << "Hello, world!\n";
}
2
2
2
2
2
2
2
2
2
2
Hello, world!
5.迭代器写法
//g++ 7.4.0
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> v(10,2);
v.push_back(3);
for(auto p=v.begin();p!=v.end();p++)
cout<<*p<<endl;
std::cout << "Hello, world!\n";
}
2
2
2
2
2
2
2
2
2
2
3
Hello, world!
6.集合使用
//g++ 7.4.0
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> s; //初始化一个集合
s.insert(1); //插入元素
s.insert(3);
s.insert(2); // 会自动排序所以输出是1,2,3
for(auto p=s.begin();p!=s.end();p++)
cout<<*p<<endl;
std::cout <<( s.find(2) != s.end()) << endl; //判断集合里面的数据是否存在
std::cout <<( s.find(4) != s.end()) << endl;
s.erase(2);// 删除集合中的2
std::cout <<( s.find(2) != s.end()) << endl; //判断集合里面的数据是否存在
}
1
2
3
1
0
0
7.键值对
//g++ 7.4.0
#include <iostream>
#include <map>
using namespace std;
int main()
{
map <string, int>m;
m["ppx"] = 1;
m["gugua"] = 1224;
cout<< m["ppx"]<<endl;
for(auto p=m.begin();p!=m.end();p++) // 迭代器输出(输出顺序和建的开头几个ascll码相关)意下面的指针
cout<<p->first << ": "<< p->second<<endl;
cout<< m.size()<<endl;//获得长度
}
1
gugua: 1224
ppx: 1
2
8.栈
//g++ 7.4.0
#include <iostream>
#include <stack> // 栈头文件
using namespace std;
int main()
{
stack<int> s;
s.push(1);// 压栈
s.push(222);
s.push(3);
// 由于栈是先进后出的,这里访问栈顶,应该就是3
cout<< s.top()<<endl;
s.pop(); // 出栈也就是删除顶上的值
cout<< s.top()<<endl;
cout<< s.size()<<endl;//获得长度
}
3
222
2
9.队列
//g++ 7.4.0
#include <iostream>
#include <queue> // 队列头文件
using namespace std;
int main()
{
queue<int> s;
s.push(1);// 入队
s.push(222);
s.push(3);
// 由于队列是先进先出的,这里可以分别访问队首和队尾
cout<< s.front()<<endl;
cout<< s.back()<<endl;
cout<<"准备出队"<<endl;
s.pop();
cout<< s.front()<<endl;
cout<< s.back()<<endl;
}
1
3
准备出队
222
3
10.集合和键值对无排序输出
//g++ 7.4.0
#include <iostream>
#include <unordered_map>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<int> s; //初始化一个集合
s.insert(1); //插入元素
s.insert(3);
s.insert(2); // 由于使用unordered_set不会自动排序所以输出是1,2,3
for(auto p=s.begin();p!=s.end();p++)
cout<<*p<<endl;
cout <<"***************" << endl;
std::cout <<( s.find(2) != s.end()) << endl; //判断集合里面的数据是否存在
std::cout <<( s.find(4) != s.end()) << endl;
s.erase(2);// 删除集合中的2
std::cout <<( s.find(2) != s.end()) << endl; //判断集合里面的数据是否存在
}
2
1
3
***************
1
0
0
11.for循环写法
//g++ 7.4.0
#include <iostream>
using namespace std;
int main()
{
int a[5] = {1};
for (int i=0;i<5;i=i+1)
{
cout<< a[i]<<endl;
}
cout<<" ________________"<<endl;
for(int i2:a)
cout<<i2<<endl;
}
1
0
0
0
0
________________
1
0
0
0
0
12.to_string
//g++ 7.4.0
#include <iostream>
#include<string>
using namespace std;
int main()
{
float a = 45.575;
cout<<to_string(a)<<endl;
}
45.575001
13.auto
自动获得类型
//g++ 7.4.0
#include <iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector <string> a(2,"ppg");
vector <int> a1(2,1);
vector <int> a2(2, 2);
string s = "ppfsa";
for(auto i:a)
cout<<i<<endl;
cout<<"____________________"<<endl;
for(string i2:a)
cout<<i2<<endl;
cout<<"auto_________________"<<endl;
for(auto p=a.begin();p!=a.end();p++)
cout<<*p<<endl;
}
ppg
ppg
____________________
ppg
ppg
auto_________________
ppg
ppg
14.stoi等相当于python的eval
//g++ 7.4.0
#include <iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int a = stoi("2432");
cout<< a-32 <<endl;
double a1 = stod("1.025");
float a2 = stof("12.545");
}