联合体语法
1 2 3 4 5 6 7
| union USER { short sHP; int nHP; };
|
实践一下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
union USER { short sHP; int nHP; double fHP; };
union { short sHP; int nHP; double fHP; } ls;
typedef struct Test { short sHP; int nHP; double fHP; };
int main() { USER user; Test t; std::cout << sizeof(user) << std::endl; std::cout << sizeof(t) << std::endl;
user.sHP = 100; std::cout << user.sHP << std::endl;
std::cout << user.nHP << std::endl; user.nHP = 0; std::cout << user.nHP << std::endl; }
|
未命名的联合体是供临时使用的!
C 语言中 字符串的拼接
1 2 3 4 5 6 7 8
| char str[0x10] = "123"; char strB[0x10] = "456";
char strC[0x20]; memcpy(strC, str, strlen(str)); memcpy(strC + strlen(str), strB, strlen(strB) + 1);
std::cout << strC;
|
C++ 中 字符串拼接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| string str{"12345"};
std::cout << str << std::endl;
string str1{ "123455", 3 }; std::cout << str1 << std::endl;
string str2{ "0123456",2,3 }; std::cout << str2 << std::endl;
string str3{ "你好啊啊啊", 3 }; std::cout << str3 << std::endl;
string str4(6, 'a'); string str5(6, 65); std::cout << str4 << std::endl; std::cout << str5 << std::endl;
string str6, str7; str6 = "123"; str7 = "456";
str6 = str7 + " " + "123"; std::cout << str6 << std::endl;
int age; std::cin >> age;
string st; st = " 用户的年龄是:"; str7 = st + std::to_string(age); std::cout << str7;
|
C++ 中字符串拼接(进阶)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include <iostream> using std::string;
#define SoftName "EDY" #define SoftVersion "2.0"
int main() { string str;
str = string{ "!22" } + "1233";
str = "abc" + ("bcd" + string{ "123" });
str += "aka"; str += "aka" + string{ "bka" };
string strB; strB = "!23""233"; strB = SoftName SoftVersion; string strC{"123"}; char a; std::cin >> a; str += a; str += a + 'o'; str += char(a + 'o'); string strD{"123"}; strD.append("456"); strD.append("222").append("dfddd"); strD.append("123455", 2); }
|
截取字符串
1 2 3 4 5 6 7 8 9 10 11
| .substr(起始位置,要截取的长度);
std::string str{"123456"}; std::string strsub{str.substr(1)}; std::string strsubA{str.substr(1, 3)};
string strB;
strB = str.substr(7).substr(3);
|
计算字符串长度
1 2 3
| string str{"222222"};
std::cout << str.length();
|
下面是一个正常计算字符串的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <iostream> #include <locale> int main() { setlocale(LC_ALL, "chs"); wchar_t wstr[255]; std::wcout << L"请输入一个字符串: "; std::wcin.getline(wstr, 255);
int length = 0; for (int i = 0; wstr[i] != L'\0'; ++i) { ++length; }
std::wcout << L"字符串的长度是: " << length << std::endl; return 0; }
|
字符串比较
1️⃣ 比较相同的内容
1 2 3 4
| char* str_1{(char*)"123456"}; char* str_2{(char*)"123456"};
std::cout << (str1 == str2) << std::endl;
|
1 2 3 4 5
| char* str_1{(char*)"123456"}; char* str_2 = new char[7]; std::cin >> str_2;
std::cout << (str1 == str2) << std::endl;
|
1 2 3 4 5
| string str_1{"123456"}; string str_2;
std::cin >> str_2; std::cout << (str1 == str2) << std::endl;
|
2️⃣ 比较不同的内容
1 2 3 4 5 6 7 8 9
| string str_1{"abcdef"}; string str_2{"bcdefg"};
if(str_1 > str_2) { std::cout << "大于"; } else std::cout << "小于等于";
|
1 2 3
| string str_1{"abcdef"}; str1.compare("bcdefg");
|
3️⃣ 截取一段之后进行比较
1 2
| string strA{"abc cdef"}; strA.compare(5,4,"cdef");
|
1 2
| string strA{"abc cdef"}; strA.compare(5,4,"cdef ghijklm", 0, 4);
|
1 2 3 4 5
| string strA{"username:5620;studentId:655555"};
std::cout << strA.find("studentId:"); std::cout << strA.substr(strA.find("studentId:")); std::cout << strA.substr(strA.find("studentId:") + 10);
|
补充 find() 方法 的用法
1
| str.find("sdajklfdkasjlkfjlas", 0, 10);
|
倒着搜索
1 2
| str.rfind("sdajklfdkasjlkfjlas", 0);
|
字符串的应用–小项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <iostream> #include <string> using std::string; int main() { string str{ "id=user;pas=632105;role=阿森;" }; string strIn; string strOut; while (true) { std::cout << "请输入您要查阅的属性:\n"; std::cin >> strIn; int lfind = str.find(strIn + "="); if (lfind == std::string::npos) std::cout << "查找的属性不存在"; else { int lend = str.find(";", lfind); strOut = str.substr(lfind + strIn.length() + 1, lend - lfind - strIn.length() - 1); std::cout << strOut << std::endl; } } }
|
插入字符串
1 2 3 4 5
| .insert(); string id{"id=;"}; id.insert(3,"testId"); std::cout << id;
|
1 2 3 4 5 6 7 8
| string id{"id="};
id.insert(3,6,'*'); std::cout << id << std::endl;
id.insert(3, "testid"); std::cout << id;
|
1 2 3 4
| string id{"id="};
id.insert(3,"killertestid", 6, 6); std::cout << id << std::endl;
|
1 2 3
| string id{"id="};
id.insert(3,"killertestid123456",6);
|
指针数组字符串
str和 str[0] 不是一个位置,具体跟数组有区别,涉及到运算符重载
str的地址和str[0]的地址差4
1 2 3
| string str{"12344"};
std::cout << (int)&str << " " << (int)&str[0] << " " << (int)&str[1];
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| string str{"12345"}; std::cout << str[0] << std::endl;
std::cout << (int)&str << " " << (int)&str[0] << " " << (int)&str[1] << std::endl; str = str + "534894539823478923";
std::cout << (int)&str << " " << (int)&str[0] << " " << (int)&str[1] << std::endl;
const char* baseStr = str.c_str();
std::cout << (int)&str << " " << (int)&str[0] << " " << (int)&str[1] << " " << (int)baseStr << std::endl;
const char* dataStr = str.data();
std::cout << (int)&str << " " << (int)&str[0] << " " << (int)&str[1] << " " << (int)baseStr << std::endl;
|
- 返回一个
const char*
指针,指向以空字符 \0
结尾的 C 风格字符串。
- 返回的字符串数据与
std::string
对象内部保持一致,但不能修改返回值(它是 const
的)。
字符串替换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| .replace(要替换的内容的起始位置,要替换的长度,“替换后的内容”);
string strId{"id=user;"};
strId.replace{3,4,"zhangsan"}; std::cout << strId;
.replace(要替换的内容的起始位置,要替换的长度,替换后的字符长度,‘字符’);
str.replace(3,4,6,'*');
.replace(要替换的内容的起始位置,要替换的长度,"替换后的内容",替换后内容的长度节选);
str.replace(3,4,"zhangsan!pkaq",8);
.replace(要替换的内容的起始位置,要替换的长度,"替换后的内容",替换后的内容的起始位置,替换后内容的长度节选);
str.replace(3,4,"zhangsan!pkaq",9,,4);
|
删除字符串内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| .erase(要删除的起始位置,要删除的起始长度);
string str{"id=user;"}; str.erase(3,4);
.erase(要删除的起始位置); string str{"id=user;"}; str.erase(3);
.erase(); .clear();
|
字符串(补充)
计算字符串长度
1 2 3 4 5 6 7 8 9 10 11 12
| string strIn; std::cin >> strIn;
int length{0}; for (int i = 0; strIn[i]; i ++ ) { if (strIn[i] < 0) i ++ ; length ++ ; }
std::cout << length;
|
字符串转换
1 2
| string cIn = "123"; int x = std::stoi(cIn);
|
字符串流
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <iostream> #include <string> #include <sstream> using std::string; using std::stringstream;
int main() { stringstream strS; strS << "你好" << "123 [" << std::hex << 12530 << "]"; string strX = strS.str(); std::cout << strX; }
|
字符串项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <iostream> #include <string> using std::string;
typedef struct Role { string Id; int Exp; }*PROLE;
int main() { string strData = "id=Tomy Clare;exp=9523;id=Sunny;exp=9523;id=DyBaby;exp=25301;id=Simple;exp=25301;id=Bkacs11;exp=2100;id=Dudu;exp=2122;";
int istart{}, iend{}, icount{}; for (int i = 0; i < strData.length(); i++) { if (strData[i] == ';') { icount++; i += 3; } } PROLE pRole = new Role[icount / 2]; icount = 0; do { istart = strData.find("id=", istart); if (istart == std::string::npos) break;
iend = strData.find(";", istart + 3); pRole[icount].Id = strData.substr(istart + 3, iend - istart - 3); istart = iend + 1; iend = strData.find(";", istart + 4);
pRole[icount++].Exp = std::stoi(strData.substr(istart + 4, iend - istart - 4)); istart = iend + 1; } while (true);
for (int i = 0; i < icount - 1; i++) { for (int j = 0; j < icount - i - 1; j++) { if (pRole[j].Exp < pRole[j + 1].Exp || (pRole[j].Exp == pRole[j + 1].Exp && pRole[j].Id > pRole[j + 1].Id)) { Role temp = pRole[j]; pRole[j] = pRole[j + 1]; pRole[j + 1] = temp; } } }
for (int i = 0; i < icount; i++) { std::cout << pRole[i].Id << " " << pRole[i].Exp << std::endl; } }
|