快速c转cpp

84 阅读4分钟
#include <iostream>//c语言引入头文件的区别 #include <string.h>
#include <cstring>
#include <string>
//命名程序空间 必须是std 建议都带上否则使用cin的时候要写成 std::cin
using namespace std;
struct  stu
{
    string id;
    int age;
};
//&a 地址的传递 a则是值传递
void fun(int &a) {
     a++;
}
int main()
{
     //C语言结构体区别 初始化的时候可以省略struct
    stu a[10];
    //常量const 不允许改变
    const int c =  10;

    //c++引用和传值
    int a = 10;
    fun(a);
    cout << a << endl;
    int n;
    ////输入n
    cin >> n;
    //输出 endl 也是std命名空间内的保留字
    cout << "Hello World!" << n << "\n";
    cout << "Hello World!" << n << endl;


    //下边写法不需要带上命名空间
    std::cin >> n;
    std::cout << "苏胜是帅B" << endl;


    ////C语言和C++结构化语句是一样的
    for (int i = 0; i < 3; i++)
    {
        cout << i;
    }
    
    //c++ 特定的boolen变量 所有非0都是true 输出结果是110
    bool flag1  = true;
    bool flag2 = 3;
    bool flag3 = 0;
    cout << flag1 << flag2 << flag3 << endl;

   
    ///c++ 多出的String 
    string s = "okk";
    string  s2 = "!!!";
    string s3 = s + s2;
    cout << s3 << endl;
     
    //c语言中的getschar
    string s4;
    getline(cin,s4);
    cout << s4;

    //c++ 字符串操作
    string s5 = "helloworld";
    cout << s5.substr(0, 1)<<endl;

  

}

```cpp

#include <iostream>
//必须要引入vector才可以使用容器
#include <vector>
//集合的头文件
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <unordered_map>
#include<unordered_set>
using namespace std;
int main()
{
	//不会对map键值对进行排序和set集合,节省排序时间,超时的时候可以考虑优化
	unordered_map <string, int> unmap;
	//unordered map 只能保证是无序的,没办法是按照插入数据的排序,按照的准则是哈希表
	return 0;
}
void queue_fun(){
	queue <int> line;
	//push(i)是入队 pop是出队 front()是访问队首 back()是访问队尾‘
	line.push(1);
}
void stack_fun(){
	//push是压栈 pop是出栈 不可以使用迭代器遍历栈
	stack <int> s;
	s.push(1);
	s.push(2)
		s.pop();
	return 0;
}
void map_fun() {
	map <string, int> m;
	m["hello"] = 1;
	m["world"] = 2;
	cout << "hello:" << m["hello"] << endl;
	for (auto p = m.begin(); p != m.end(); p++)
	{
		//first是键 second是值 是按照键ascii排序  
		cout << p->first << ":" << p->second << endl;
	}
}
void set_fun() {	//类似一个数组 大小为10 默认全都是为0
	vector <int> v(10);
	//初始化大小为10,并且值为2
	vector <int> v1(10, 2);
	vector <int> v3(10);
	v3.resize(13);
	cout << v3.size() << endl;
	/*for (int i = 0; i < v.size(); i++)
	{
		cout << v3[i] << "\t";
	}*/
	//迭代器的遍历数组 本质上可以看成一个指针 v.end()最后数据后一个
	for (auto p = v3.begin(); p != v3.end(); p++) {
		cout << *p << "\t";
	}
	cout << endl;
	cout << v.size() << endl;
	for (int i = 0; i < v.size(); i++)
	{
		v[i] = i;
		cout << v[i] << "\t";
	}
	cout << endl;
	v.push_back(11);
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << "\t";
	}
	cout << endl;
	//初始化的时候不可以赋初值
	set <int> s;
	//集合插入数据      
	s.insert(1);
	s.insert(2);
	//删除对应的值不是位置
	s.erase(1);
	//查找find()返回的是一个指针
	cout << (s.find(1) != s.end()) << endl;
	//set会自动排序
        }
}
// ConsoleApplication3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
//好使用的二进制
#include <bitset>
//sort函数
#include <algorithm>
//数组
#include <vector>
//字符串操作
#include <cctype>
//to_string
#include <string>
using namespace std;
//cmp 不允许出现=号 否则会出现段错误  
bool cmp(int x, int y) {
    return x > y;
}
int main()
{
    //auto 自动识别类型,必需初始化才可以使用cin输入
    //容器使用时候得到迭代器 栈和队列不行使用迭代器
    //限制范围的for循环
    int a[5] = {1};
    for (auto i:a) {
        cout << i << "\t";//1 0 0 0 0 
    }
    cout << endl;
    //to_string stoi stof stold stol stoll stouul stoull 
    int str_i = stoi("123");
    cout << str_i << endl;
    string s = to_string(123);
    cout << s << endl;

}   
void str_fun() {
    char c = 'A';
    cout << "C:" << c << endl;
    cout << "判断是不是字母" << isalpha(c) << endl;
    cout << "判断是不是大写" << isupper(c) << endl;
    cout << "判断是不是小写" << islower(c) << endl;
    //isalnum输出表示真不一定是1还有可能是其他数字
    cout << "判断是不是数字" << isalnum(c) << endl;
    cout << "判断是不是space,\\t \\n \\" << isspace(c) << endl;
    //相对应的还有toupper
    cout << (char)tolower(c) << endl;
}
void sort_fun() {
    vector <int> m(10);
    bool flag = false;
    for (int i = 0; i < 10; i++)
    {
        m[i] = -i;
    }
    for (auto p = m.begin(); p != m.end(); p++)
    {
        cout << *p << "\t";
    }
    cout << endl;
    //排序默认是从小到大
    sort(m.begin(), m.end(), cmp);

    for (auto p = m.begin(); p != m.end(); p++)
    {
        cout << *p << "\t";
    }
}
void bitset_fun() {
    //表示二进制位数为5,初始化二进制数为19
    //bitset可以看成字符数组
    bitset <5> b(19);
    cout << b << endl;//10011
    bitset <5> b1("11");
    cout << b1 << endl;///00011

    //bitset的处理
    cout << "any() b1是不是存在1 " << b1.any() << endl;
    cout << "none() b1是不是不存在1 " << b1.none() << endl;
    cout << "count() b1中1的个数" << b1.count() << endl;
    cout << "size() b1中的大小" << b1.size() << endl;
    cout << "test() b1中选中下标的位置是不是1 " << b1.test(2) << endl;
    //不加参数全部取反
    cout << b1.flip() << endl;
    //从高位开始,就是从右侧开始取反
    cout << b1.flip(1) << endl;
    //转成unsigned long
    unsigned long a = b.to_ullong();
    cout << a << endl;//输出19
    std::cout << "Hello World!\n";

    string str = "0110101";
    bitset <5> b3(str, 2, 5);
    bitset <5> b4("01101");
    cout << b3 << endl;
    cout << b4 << endl;
}