system应用,写一个cmd

int main()
{
	system("cd");
	string cmd = "cd";
	system(cmd.c_str());//列出目录下的文件

	cout << ">>";
	cin >> cmd;
	system(cmd.c_str());
	cout << "缓冲区in_avail:" << cin.rdbuf()->in_avail() << endl;
	system("pause");
}

在这里插入图片描述

进入目录
在这里插入图片描述

argc,和*argv[]

int main(int argc, char *argv[])
{
	cout << "argc = " << argc << endl;//1个参数

	//第一个参数是执行程序完整路径
	cout << "argv = " << argv[0] << endl;//返回当前路径

	//第二个参数是用户传递
	//testmain.exe deguf info

	if (argc > 1) cout << "argv[1] = " << argv[1] << endl;
	if (argc > 2) cout << "argv[2] = " << argv[2] << endl;
	system("pause");
}

在这里插入图片描述

枚举enum

enum 枚举类型名
{
	枚举值1,//默认0
	枚举值2,//默认1
	枚举值3,//默认2
	枚举值4=1000,
	枚举值5//默认1001
};
int main()
{
	枚举类型名 ev1{ 枚举值1 };
	cout << "ev1:" << ev1 << endl;
	枚举类型名 ev2{ 枚举值2 };
	cout << "ev2:" << ev2 << endl;
	枚举类型名 ev4{ 枚举值4 };
	cout << "ev4:" << ev4 << endl;
	枚举类型名 ev5{ 枚举值5 };
	cout << "ev5:" << ev5 << endl;
	

	system("pause");
}

在这里插入图片描述

enum States
{
	PLAY,
	PAUSE,
	STOP,
};
int main()
{

	States status{ STOP };
	if (status == STOP)
	{
		status = PLAY;
	}
	cout << "status:" << status << endl;
	if (status == PLAY)
	{
		cout << "Playing..."  << endl;
	}
	system("pause");
}

在这里插入图片描述

enum class LogLevel
{
	DEBUG,
	INFO,
	ERROR,
	FATAL
};

int main()
{
	//LogLevel level{ DEBUG };//报错,容易出现冲突。改成下边
	LogLevel level_1{ LogLevel::DEBUG };
	cout << "level = " << (int)level_1 << endl;
	LogLevel level_2{ LogLevel::ERROR };
	cout << "level = " << (int)level_2 << endl;

	LogLevel conflevel{ LogLevel::INFO };
	if (level_2 >= conflevel)
	{
		cout << "log view " << endl;
	}
	
	system("pause");
}

在这里插入图片描述

与或非

#include<bitset>
int main() {
	char a = 0b10000001;
	char b = 0b00000001;
	cout << "a: " << bitset<8>(a) << endl;
	cout << "~a:" << bitset<8>(~a) << endl;
	cout << "b: " << bitset<8>(b) << endl;
	cout << "~b:" << bitset<8>(~b) << endl;
	cout << "a&b: " << bitset<8>(a&b) << endl;
	cout << "a|b: " << bitset<8>(a|b) << endl;
	
	system("pause");
}

在这里插入图片描述

string字符串substr、.empty()

#include<string>
int main() {
	
	string str1{ "teststring 1" };
	cout << "str1: "<<str1 << endl;
	cout << size(str1) << endl;
	string str2{ "teststring 2" };
	string str3 = "123456789";
	string str4 = "一二三四五六七八九";
	cout << "str3: " << str3 <<" size: "<<str3.size()<< endl;
	cout << "str4: " << str4 <<" size: "<<str4.size() << endl;
	cout << str1.substr(1) << endl;//取部分字符串
	cout << str1.substr(0,5) << endl;//取部分字符串
	cout << str1.empty() << endl;//判断字符串是否为空
	
	system("pause");
}

在这里插入图片描述

字符到数字的转换、数字到字符的转换stod、stof、stoll、to_string、字符串拼接

int main() {
	//字符串—>数字的转换
	auto i1 = stoi("1234");
	cout << "i1=" << i1 << endl;
	cout << stod("123.5") << endl;
	cout << stof("123.44f") << endl;
	cout << stoll("-12344") << endl;
	//和数字—>字符串的转换
	auto pi = to_string(3.1415926);
	cout << "pi = " << pi << endl;
	cout << to_string(1024) << endl;
	//字符串拼接
	string log;
	string txt = "login system";
	string user = "zhangsan";
	int threadid = 1023;
	log = user + ":" + txt + ":" + to_string(threadid);
	cout << log << endl;
	log = "[debug" + log;
	log += ";";
	cout << log << endl;

	system("pause");
}

在这里插入图片描述

字符串查找和替换replace、string::npos

int main() {
	//查找和替换
	string strfind = "test for find [user] login system";
	cout << strfind << endl;
	auto pos = strfind.find("[test]");
	if (pos == string::npos)//没找到
	{
		cout << "[test] not found!" << endl;
	}
	string key{ "[user]" };
	pos = strfind.find(key);
	if (pos != string::npos)//能找到
	{
		cout << "[user] find!" << endl;
		cout << pos << endl;//14
		cout << strfind.substr(pos+key.size()) << endl;
		//替换字符串
		strfind.replace(pos, key.size(), "root");
		cout << strfind << endl;
	}
	system("pause");
}

在这里插入图片描述

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐