C++:string

一、string概念
之前介绍过通过字符数组保存字符串,然后对字符数组中的字符串做各种操作;为了更加简单方便,在C++中,又增加了 string 来处理字符串。
char str[20] = "hello world";
string 字符串其实是一种更加高级的封装, string 字符串中包含大量的方法,这些方法使得字符串的操作变得更加简单。那到底什么是string呢?C++中将字符串直接作为一种类型,也就是 string 类型,使用  string 类型创建的对象就是C++ 的字符串。
string s1;
string s2 = "abc";
使用 C++ 中提供的 string 时,必须添加头文件 <string>。
二、string 常见操作
1. 创建字符串
string s1;//创建空字符串
string s2 = "abc";//创建字符串
#include <iostream>
#include <string> //添加string头文件
using namespace std;
int main()
{string s1;string s2 = "hello world";cout << "s1:" << s1 << endl; //s1:cout << "s2:" << s2 << endl; //s2:hello worldreturn 0;
}
创建字符串的方式与前面学习到创建内置数据类型的方式相同,只是这里的字符串类型为 string
(1)string s1 表示创建空字符串,相当于创建整型 int a ,但未给 a 一 个初始值。
(2) string s2 = "hello world" 表示创建一个字符串s2,它的内容是" hello world ",要 注意 s2 中的字符串不再以 \0 作为结束标志了。(C 语言风格的字符串是以 \0 作为结束标志 的)后面还会介绍别的创建字符串的方法,但这种方法是最常见的。
上面这个图,仅仅是字符串s2的示意图,实际上 string 类型的字符串比这个要复杂的多,我们可以大概这样去理解,更多的知识需要学习C++的类和对象的知识才能明白。
当然C++中的 string 创建的字符串和 char 类型的数组所表示的字符串还有一个区别, string 类型的字符串对象可以直接赋值,比如:
#include <iostream>
#include <string>
using namespace std;
int main()
{string s1("hello world");string s2("hehe");s2 = s1;cout << s2 << endl;return 0;
}

除了以上创建字符串的写法外,C++中还有一些其他的创建字符串方式。如:

string s("hello world"); //等同于string s1 = "hello world";
string s1 = s; //⽤⼀个现成的字符串s,初始化另外⼀个字符串s1
2. string字符串的输入
(1) cin的方式
可以直接使用  cin string 类型的字符串中输入一个字符串的数据。
#include <iostream>
#include <string>
using namespace std;
int main()
{string s;//输入 cin >> s;//输出cout << s << endl;return 0;
}
其实 cin 的方式给 string 类型的字符串中输入数据的时候,可以输入不带空格的字符串。但是如果带有空格,遇到空格也就读取结束了,没有办法正常读取,那怎么办呢?解决办法就是使用 
getline 。
(2) getline 的方式
getline 是C++标准库中的一个函数,用于从输入流中读一行文本,并将其存储为字符串。getline 函数有两种不同的形式,分别对应着字符串的结束方式。
istream& getline (istream& is, string& str);
istream& getline (istream& is, string& str, char delim);
istream 是输入流类型, cin 是 istream 类型的标准输入流对象。
ostream 是输出流类型, cout 是 ostream 类型的标准输出流对象。
getline 函数是输入流中读取一行文本信息,所有如果是在标准输入流(键盘)中读取数据,就可以传 cin 给第一个参数。
第一种 getline 函数以换行符( '\n' )作为字符串的结束标志,它的一般格式是:
getline(cin, string str)
//cin -- 表⽰从输⼊流中读取信息
//str 是存放读取到的信息的字符串
这种形式的 getline 函数从输入流( 例如 cin )中 读取文本,直到遇到换行符( '\n' )为止,然后将读取到的文本(不包括换行符)存储到指定的 string 类型的变量 str 中。
#include <iostream>
#include <string>
using namespace std;
int main ()
{string name;getline (cin, name);cout << name << endl;return 0;
}
第二种 getline 函数允许用户自定义结束标志,它的一般格式是:
getline(cin, string str, char delim)
//cin -- 表⽰从输⼊流中读取信息
//str 是存放读取到的信息的字符串
//delim 是⾃定义的结束标志
这种形式的 getline 函数从输入流中读取文本,直到遇到用户指定的结束标志字符( delim )为止,然后将读取到的文本(不包括结束标志字符)存储到指定的 string 类型的变量 str 中。
#include<iostream>
#include <string>
using namespace std;
int main ()
{string name;getline (cin, name, 'q');cout << name << endl;return 0;
}
在使用C++中的 string 字符串时,想要输入的字符串中包含空格,那么 getline 函数就是必须的。在竞赛中为了方便处理字符串,通常会使用 string 类型的字符串,所以在字符串输入的时候 getline 就很常见。
3. size()
string 中提供了 size() 函数用于获取字符串长度。
使用示例:
#include <iostream>
#include <string> //添加string头⽂件
using namespace std;
int main()
{string s;string s1 = "hello";string s2 = "hello world";string s3 = "12ab!~ "; cout << "s:" << s.size() << endl;cout << "s1:" << s1.size() << endl;cout << "s2:" << s2.size() << endl;cout << "s3:" << s3.size() << endl;return 0;
}
需要注意的是,像 char int double 等内置类型的数据在操作的时候,我们是不会使用 . 操作符的。string 是 C++提供的一种更加复杂的封装类型,在 string 类型的变量中加入了操作这个字符串的各种方法(函数), 比如求字符串长度、字符串末尾插入⼀个字符等操作。所以要对 string 类型的变量进行各种操作,就可以使用  . 操作符来使用这些函数。
我们可以看下⾯的例⼦,⼤家要仔细体会。
通过 size() 能获得字符串的长度,顺便就可以使用这个长度遍历字符串的,注意 string 类型的字符串是可以通过下标访问的,比如:
#incldue <iostream>
#include <string>
using namespace std;
int main()
{string s = "abcdef";int i = 0;for(i = 0; i < s.size(); i++){cout << s[i] << " ";}return 0;
}

4.迭代器(iterator)

迭代器是一种对象,它可以用来遍历容器(比如 string )中的元素,迭代器的作用类似于指针,或者数组下标。访问迭代器指向的值,需要解引用(*)。
C++ 中的 string 提供了多种迭代器,用于遍历和操作字符串中的内容。这里给大家介绍一种最常
用的迭代器。
begin() end()
begin() :返回指向字符串第一个字符的迭代器,需要一个迭代器的变量来接收。
end() :返回指向字符串最后一个字符的下一个位置的迭代器(该位置不属于字符串)。
string begin() end() 返回的迭代器的类型是 string::iterator
迭代器是可以进行大小比较,也可以进行  + 或者 - 整数运算的。
比如: it++ 就是让迭代器前进一步, it-- 就是让迭代器退后一步。
同一个容器的两个迭代器也可以相减,相减结果的绝对值,是两个迭代器中间元素的个数。
#include <iostream>
#include <string>
using namespace std;
int main()
{string s = "abcdef";string::iterator it1 = s.begin();string::iterator it2 = s.end();cout << (it1 < it2) << endl;cout << it1 - it2 << endl;return 0;
}
迭代器通常用于遍历字符串的,可以正序遍历,也可以逆序遍历。
正序遍历:
#include <iostream>
#include <string>
using namespace std;
int main()
{string s = "abcdef"; //auto it 是让编译器⾃动推到it的类型for (auto it = s.begin(); it != s.end(); ++it) { cout << *it << ' '; }//string::iterator 是正向迭代器类型//string::iterator it,是直接创建迭代器,it是针对字符串的迭代器for (string::iterator it = s.begin(); it != s.end(); ++it) { cout << *it << ' '; }return 0;
}
逆序遍历:
#include <iostream>
#include <string>
using namespace std;
int main()
{string s = "abcdef"; for (string::iterator it = s.end() - 1; it >= s.begin(); --it) { cout << *it << ' '; }return 0;
}
通过迭代器找到元素后,改变迭代器指向的元素,是可以直接改变字符串内容的。
#include <iostream>
#include <string>
using namespace std;
int main()
{string str = "abcdef"; cout << str << endl;for (string::iterator it = str.begin(); it != str.end(); ++it) { *it = 'x'; }cout << str << endl;return 0;
}
5.push_back()
push_back() 用 于在字符串尾部插一个字符。
使用示例:
#include <iostream>
#include<string> //添加string头⽂件
using namespace std;
int main()
{//向空字符串中尾插字符string s;s.push_back('h');s.push_back('e');s.push_back('l');s.push_back('l');s.push_back('o');cout << s << endl;//向⾮空字符串中尾插字符string s1 = "hello ";s1.push_back('w');s1.push_back('o');s1.push_back('r');s1.push_back('l');s1.push_back('d');cout << s1 << endl;//批量插⼊字符string s2;for (char c = 'a'; c <= 'f'; c++){s2.push_back(c);}cout << s2 << endl;return 0;
}
6.字符串的+=和+运算
push_back() 是用于在字符串后添加一个字符,然而部分情况下我们需要向原有的字符串后继续添
加字符串。
其实 string 类型的字符串是支持 + += 运算的。这里的本质是 string 中重载了operator+= 这个操作符。
具体使用请看下面的示例:
#include <iostream>
#include <string> //添加string头⽂件
using namespace std;
int main()
{string s = "hello";s += " world"; //字符串⽤双引号,等价于 s = s + " world"cout << s << endl;//除了+=操作,也可以使⽤'+'灵活进⾏字符串拼接//1.尾部拼接string s1 = "hello";cout << s1 + " world" << endl; //s1仍然是"hello" s1 = s1 + " world";cout << s1 << endl; //s1是"hello world" //2.头部拼接string s2 = "hello";s2 = "world " + s2 ; cout << s2 << endl; //s2为:"world hello" return 0;
}
7.pop_back()
pop_back() 用 于删除字符串中尾部的一个字符。这个成员函数是在 C++11 标准中引入的,有的编
译器可能不支持。
使用示例:
#include <iostream>
#include<string>
using namespace std;
int main()
{string s = "hello";cout << "s:" << s << endl;//尾删s.pop_back();cout << "s:" << s << endl;//尾删s.pop_back();cout << "s:" << s << endl;return 0;
}
但是当字符串中没有字符的时候,再调用  pop_back() 时,程序会出现异常。这种行为也是未定义
行为,要避免使用。
#include <iostream>
#include <string> //添加string头⽂件
using namespace std;
int main()
{string s;s.pop_back();return 0;
}
在DevC++上,程序最终崩溃了。
为避免循环删除导致对空字符串进行尾删,可以将删除全部字符的代码写成:
#include <iostream>
#include<string> //添加string头⽂件
using namespace std;
int main()
{string s = "abc";while(s.size() > 0) //通过size()函数来控制字符串的⻓度{s.pop_back();}return 0;
}
8.insert
如果我们需要在字符串中间的某个位置插入一个字符串,怎么办呢?这时候我们得掌握一个函数就是 insert。
函数原型如下:
string& insert (size_t pos, const string& str); //pos位置前⾯插⼊⼀个string字符串
string& insert (size_t pos, const char* s); //pos位置前⾯插⼊⼀个C⻛格的字符串
string& insert (size_t pos, size_t n, char c);//pos位置前⾯插⼊n个字符c
代码举例:
#include <iostream>
#include <string>
using namespace std;
int main()
{string s = "abcdefghi";string str = "xxx";cout << s << endl;s.insert(3, str);cout << s << endl;return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{string s = "abcdefghi";cout << s << endl;s.insert(3, "xxx");cout << s << endl;return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{string s = "abcdefghi";cout << s << endl;s.insert(3, 3, 'x');cout << s << endl;return 0;
}
9.find()
find() 函数用于查找字符串中指定子串/字符,并返回子串/字符在字符串中第一次出现的位置。
size_t find (const string& str, size_t pos = 0) const;
//查找string类型的字符串str,默认是从头开始查找,pos可以指定位置开始
size_t find (const char* s, size_t pos = 0) const;
//查找C⻛格的字符串s,默认是从头开始查找,pos可以指定位置开始
size_t find (const char* s, size_t pos, size_t n) const;
//在字符串的pos这个位置开始查找C⻛格的字符串s中的前n个字符,
size_t find (char c, size_t pos = 0) const;
//查找字符c,默认是从头开始,pos可以指定位置开始
返回值:
若找到,返回子串/字符在字符串中第一次出现的起始下标位置。
若未找到,返回一个整数 值 npos (针对 npos 的介绍 会在下面给出)。通常判断 find() 函数的返回值是否等于 npos 就能知道是否查找到子串或者字符。
代码举例:
//代码1
#include <iostream>
#include <string> //添加string头⽂件
using namespace std;
int main()
{string s = "hello world hello everyone";string str = "llo";//查找string类型的字符串size_t n = s.find(str);//省略第二个参数,默认为0,从头开始cout << n << endl;n = s.find(str, n + 1); //从n+1这个指定位置开始查找cout << n << endl;//查找C⻛格的字符串n = s.find("llo");//省略第二个参数,默认为0,从头开始cout << n << endl;n = s.find("llo", n + 1); //从n+1这个指定位置开始查找cout << n << endl;return 0;
}
//代码2
#include <iostream>
#include <string> //添加string头⽂件
using namespace std;
int main()
{string s = "hello world hello everyone";//在s中,0这个指定位置开始查找"word"中的前3个字符size_t n = s.find("word", 0, 3);cout << n << endl;n = s.find("everyday", n+1, 5);cout << n << endl;
return 0;
}
//代码3
#include <iostream>
#include <string> //添加string头⽂件
using namespace std;
int main()
{string s = "hello world hello everyone";size_t n = s.find('o');cout << n << endl;n = s.find('o', n + 1);cout << n << endl;return 0;
}
//查找不到的情况
#include <iostream>
#include <string> //添加string头⽂件
using namespace std;
int main()
{string s = "hello world hello everyone";string str = "bit";size_t n = s.find(str);cout << n << endl;if(n != string::npos)cout << "找到了,位置是:" << n << endl;elsecout << "没有找到" << endl;return 0;
}
在字符串中查找字符或者字符串时,有可能查找不到,这时候 find 函数会返 回 npos 这个值 ,该数
字并不是一个随机的数字,而是 string 中定义的一个静态常量 npos 。我们通常会判断 find 函数的返回值是否等于 npos 来判断,查找是否成功。
static const size_t npos = -1;
打印出来看看:
#include <iostream>
#include <string> //添加string头⽂件
using namespace std;
int main()
{//注意:npos是string中定义的,使⽤npos需要带上string::指明是string类中的cout << "npos:" << string::npos << endl; //npos:18446744073709551615return 0;
}
10.substr()
substr() 函数用于截取字符串中指定位置指定长度的子串。函数原型如下:
string substr (size_t pos = 0, size_t len = npos) const;
//pos 的默认值是0,也就是从下标为0的位置开始截取
//len 的默认值是npos,意思是⼀直截取到字符串的末尾
substr() :如果函数不传参数,就是从下标为0的位置开始截取,直到结尾,得到的是整个字符串;
substr(pos) :从指定下标 pos 位置开始截取子串,直到结尾;
substr(pos, len) :从指定下标 pos 位置开始截取长度为 len 的子串。
返回值类型: string ,返回的是截取到的字符串,可以使用  string 类型的字符串接收。
代码举例:
#include <iostream>
#include<string> //添加string头⽂件
using namespace std;
int main()
{string s = "hello world hello everyone";string s1 = s.substr(7);cout << s1 << endl;string s2 = s.substr(7, 6);cout << s2 << endl;return 0;
}
substr() find() 经常是配合使用的, find 负责找到位置, substr 从这个位置向后获得字符串。
#include <iostream>
#include<string> //添加string头⽂件
using namespace std;
int main()
{string s = "hello world hello everyone";size_t n = s.find("world");string s2 = s.substr(n, 10);cout << s2 << endl;return 0;
}
11.string的关系运算
在实际写代码的过程中经常会涉及到两个字符串比较大小,比如:判断你输入的密码是否正确,就得将输入的密码和数据库中正确的密码比较。
那么两个 string 类型字符串是否可以比较大小呢?其实是可以的,C++中为string提供了一系列的关系运算。
支持的关系运算:
string s1 = "abc";
string s2 = "abcd";
char s3[] = "abcdef"; //C⻛格的字符串
(1) s1 == s2
bool operator== (const string& lhs, const string& rhs);//使⽤⽅式:s1 == s2
bool operator== (const char* lhs, const string& rhs);//使⽤⽅式:s3 == s1
bool operator== (const string& lhs, const char* rhs);//使⽤⽅式:s1 == s3
(2) s1 != s2
bool operator!= (const string& lhs, const string& rhs);//使⽤⽅式:s1 != s2
bool operator!= (const char* lhs, const string& rhs);//使⽤⽅式:s3 != s1
bool operator!= (const string& lhs, const char* rhs);//使⽤⽅式:s1 != s3
(3) s1 < s2
bool operator< (const string& lhs, const string& rhs);//使⽤⽅式:s1 < s2
bool operator< (const char* lhs, const string& rhs);//使⽤⽅式:s3 < s1
bool operator< (const string& lhs, const char* rhs);//使⽤⽅式:s1 < s3
(4) s1 <= s2
bool operator<= (const string& lhs, const string& rhs);//使⽤⽅式:s1 <= s2
bool operator<= (const char* lhs, const string& rhs);//使⽤⽅式:s3 <= s1
bool operator<= (const string& lhs, const char* rhs);//使⽤⽅式:s1 <= s3
(5) s1 > s2
bool operator> (const string& lhs, const string& rhs);//使⽤⽅式:s1 > s2
bool operator> (const char* lhs, const string& rhs);//使⽤⽅式:s3 > s1
bool operator> (const string& lhs, const char* rhs);//使⽤⽅式:s1 > s3
(6) s1 >= s2
bool operator>= (const string& lhs, const string& rhs);//使⽤⽅式:s1 >= s2
bool operator>= (const char* lhs, const string& rhs);//使⽤⽅式:s3 >= s1
bool operator>= (const string& lhs, const char* rhs);//使⽤⽅式:s1 >= s3
上述的关系运算符的重载看着复杂,但是使用起来是非常简单的。
关于操作符的重载,只有深入学习C++的类和对象,才能深入理解和掌握,在竞赛中不需要深入挖掘,会使用就行,但是要使用C++进行工程性开发的时候这部分知识一定要补上。
注:字符串的比较是基于字典序进行的,比较是对应位置上字符的ASCII值的大小;比较的不是字符串的长度。
比如
"abc" < "aq" //'b'的ascii码值是⼩于'q'的
"abcdef" < "ff" //'a'的ASCII码值是⼩于'f'的
"100" < "9" //'1'的ASCII码值是⼩于'9'的
代码举例1:
#include <iostream>
#include<string>
using namespace std;
int main()
{string s1 = "hello world";string s2 = "hello";if (s1 == (s2 + " world")) {cout << "s1 == s2" << endl;}else{cout << "s1 != s2" << endl;}return 0;
}
代码举例2:
#include <iostream>
#include <string> 
using namespace std;
int main()
{string s1 = "abcd";string s2 = "abbcdef";char s3[] = "bbc";if (s1 > s2)cout << "s1 > s2" << endl;elsecout << "s1 <= s2" << endl;if (s1 == s2)cout << "s1 == s2" << endl;elsecout << "s1 != s2" << endl;if (s1 <= s3)cout << "s1 <= s3" << endl;elsecout << "s1 > s3" << endl;return 0;
}
12.和string相关的函数
(1)stoi/stol
stoi 是将字符串转换成 int 类型的值
stol 是将字符串转换成 lon g int 类型的值
这里以 stoi 为例讲解一下这两函数的使用方式。
stoi 函数其实可以将一个 string 类型的字符串,转化为整型,函数原型如下:
int stoi (const string& str, size_t* idx = 0, int base = 10);
long stol (const string& str, size_t* idx = 0, int base = 10);
str 表示被转换的 string 类型的字符串。
idx 是一个输出型参数,也就是这个通过这个参数会带回一个值。 idx 是一个指针,需要在外边创建一个 size_t 类型的值,传递它的地址给 idx ,这个参数将会带回 str 中无法正确匹配数字的第一个字符的位置。如果不想使用这个参数,可以将这个参数设为NULL或0。
base 表示被解析的字符串中数字的进制值,可能是 2 , 8 , 10 , 16 或者 0 。
默认情况下这个值是 10 ,表示  10 进制数字
如果传递的是 2 ,表示被解析的字符串中是 2 进制的数字,最终会转换成 10 进制
如果传递的是 8 ,表示被解析的字符串中是 8 进制的数字,最终会转换成 10 进制
如果传递的是 16 ,表示被解析的字符串中是 16 进制的数字,最终会转换成 10 进制
如果传递的是 0 ,会根据字符串的内容的信息自动推导进制,比如:字符串中有 0x ,就认为
16 进制, 0 开头会被认为是 8 进制,最终会转换成 10 进制。
代码举例:
#include <iostream>
#include<string> 
using namespace std;
int main()
{size_t pos = 0;string s1 = "11x34";int ret1 = stoi(s1, &pos, 16);cout << ret1 << endl;cout << "pos:" << pos << endl;string s2 = "11x34";int ret2 = stoi(s2, &pos, 2);cout << ret2 << endl;cout << "pos:" << pos << endl;string s3 = "0x11x34";int ret3 = stoi(s3, &pos, 0);cout << ret3 << endl;cout << "pos:" << pos << endl;return 0;
}
(2)stod/stof
stod 是将字符串转换成 double 类型的值,函数原型如下,和 stoi 函数的比较的话,少了描述
字符串中数字进制的参数,其他参数一致。 stof 是将字符串转换成 flaot 类型的值。
double stod (const string& str, size_t* idx = 0);
float stof (const string& str, size_t* idx = 0);
代码举例:
#include <iostream>
#include<string> 
using namespace std;
int main()
{string s = "3.14x456";double ret = stod(s, NULL);cout << ret << endl;return 0;
}
(3)to_string
函数原型如下:
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
to_string 函数可以将数字转换成字符串,从上述函数原型的角度看的话,可以将整型、浮点型的数
字转换成字符串的,使用起来也非常简单。
代码举例:
#include <iostream>
#include <string> 
using namespace std;
int main()
{string pi = "pi is " + to_string(3.14159);cout << pi << endl;return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/1739.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STORM:从多时间点2D图像中快速重建动态3D场景的技术突破

随着计算机视觉和机器学习技术的迅猛发展,我们已经能够利用AI来解决许多复杂的问题。然而,在处理大规模室外动态3D场景重建时,现有的方法往往面临着诸多挑战,如需要大量人工标注数据、处理速度慢以及难以准确捕捉移动物体等。为了解决这些问题,研究者们开发了STORM(Spati…

C#使用OpenTK绘制3D可拖动旋转图形三棱锥

接上篇,绘制着色矩形 C#使用OpenTK绘制一个着色矩形-CSDN博客 上一篇安装OpenTK.GLControl后,这里可以直接拖动控件GLControl 我们会发现GLControl继承于UserControl //// 摘要:// OpenGL-aware WinForms control. The WinForms designer will always call the default//…

Vue2+OpenLayers使用Overlay实现点击获取当前经纬度信息(提供Gitee源码)

目录 一、案例截图 二、安装OpenLayers库 三、代码实现 关键参数&#xff1a; 实现思路&#xff1a; 核心代码&#xff1a; 完整代码&#xff1a; 四、Gitee源码 一、案例截图 二、安装OpenLayers库 npm install ol 三、代码实现 覆盖物&#xff08;Overlay&#xf…

浅谈云计算12 | KVM虚拟化技术

KVM虚拟化技术 一、KVM虚拟化技术基础1.1 KVM虚拟化技术简介1.2 KVM虚拟化技术架构1.2.1 KVM内核模块1.2.2 用户空间工具&#xff08;QEMU、Libvirt等&#xff09; 二、KVM虚拟化技术原理2.1 硬件辅助虚拟化2.2 VMCS结构与工作机制 三、KVM虚拟化技术面临的挑战与应对策略3.1 性…

【2024年华为OD机试】(C卷,100分)- 分割均衡字符串 (Java JS PythonC/C++)

一、问题描述 题目描述 均衡串定义&#xff1a;字符串中只包含两种字符&#xff0c;且这两种字符的个数相同。 给定一个均衡字符串&#xff0c;请给出可分割成新的均衡子串的最大个数。 约定&#xff1a;字符串中只包含大写的 X 和 Y 两种字符。 输入描述 输入一个均衡串…

【update 更新数据语法合集】.NET开源ORM框架 SqlSugar 系列

系列文章目录 &#x1f380;&#x1f380;&#x1f380; .NET开源 ORM 框架 SqlSugar 系列 &#x1f380;&#x1f380;&#x1f380; 文章目录 系列文章目录前言 &#x1f343;一、实体对象更新1.1 单条与批量1.2 不更新某列1.3 只更新某列1.4 NULL列不更新1.5 无主键/指定列…

项目概述、开发环境搭建(day01)

软件开发整体介绍 软件开发流程 第1阶段: 需求分析 需求规格说明书&#xff0c; 一般来说就是使用 Word 文档来描述当前项目的各个组成部分&#xff0c;如&#xff1a;系统定义、应用环境、功能规格、性能需求等&#xff0c;都会在文档中描述。产品原型&#xff0c;一般是通过…

数据仓库基础常见面试题

1.数据仓库是什么 ‌数据仓库&#xff08;Data Warehouse&#xff09;是一个面向主题的、集成的、非易失的、随时间变化的数据集合&#xff0c;用于支持企业的管理决策‌。它不同于传统的操作型数据库&#xff0c;后者主要用于处理日常业务交易和实时查询&#xff0c;而数据仓库…

shell脚本回顾1

1、shell 脚本写出检测 /tmp/size.log 文件如果存在显示它的内容&#xff0c;不存在则创建一个文件将创建时间写入。 一、 ll /tmp/size.log &>/dev/null if [ $? -eq 0 ];then cat /tmp/size.log else touch /tmp/size.log echo date > /tmp/size.log fi二、 if …

【C++】PP5015 [NOIP2018 普及组] 标题统计

博客主页&#xff1a; [小ᶻ☡꙳ᵃⁱᵍᶜ꙳] 本文专栏: C 文章目录 &#x1f4af;前言&#x1f4af;题目背景题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1 样例 #2样例输入 #2样例输出 #2 提示数据规模与约定 &#x1f4af;方法分析方法1&#xff1a;我的做法实…

【C++第三方库】快速上手---轻量级数据库SQLite和单元测试工具Gtest

每日激励&#xff0c;“驾驭命运的舵是奋斗。不抱有幻想&#xff0c;不放弃一点机会&#xff0c;不停止一日努力。” 绪论​&#xff1a; 本篇文章将写道如何快速的上手Gtest和SQLite第三方库&#xff0c;这两个第三方库都是在项目编写过程中非常重要的。 —————— 话不多说…

《机器学习》——贝叶斯算法

贝叶斯简介 贝叶斯公式&#xff0c;又称贝叶斯定理、贝叶斯法则&#xff0c;最初是用来描述两个事件的条件概率间的关系的公式&#xff0c;后来被人们发现具有很深刻的实际意义和应用价值。该公式的实际内涵是&#xff0c;支持某项属性的事件发生得愈多&#xff0c;则该属性成…

优先级队列(算法十四)

简介 优先级队列其实就是堆 默认大根堆 小根堆&#xff1a;greater<T> std::priority_queue<int, std::vector<int>, std::greater<int>> pq; priority_queue 没有迭代器&#xff0c; 不能for&#xff08;auto e:pq); 不改变原来pq&#xff0c;查…

【day5】Redis持久化之AOF + Redis事务_锁机制

AOF是什么 以日志的形式来记录每个写操作(增量保存)&#xff0c;将 Redis 执行过的所有写指令记录下来(比 如 set/del 操作会记录, 读操作 get 不记录 只许追加文件但不可以改写文件 redis 启动之初会读取该文件重新构建数据 redis 重启的话就根据日志文件的内容将写指令从前到…

C#补充----反射,特性,迭代器,特殊语法,值类型运用类型。

1.反射。 《1》获取类的方式 《2》反射的应用 <1>获取类型的所有公共成员 <2>获取构造函数 <3>获取类型的 公共成员变量 <4>获取类型的 公共方法 <5>.获取类型的 属性 <6>.公共接口&#xff0c;公共枚举&#xff0c;公共事件

MyBatis——XML映射文件

在MyBatis中&#xff0c;既可以通过注解的方式配置SQL语句&#xff0c;也可以通过XML映射文件的方式配置SQL语句。对于简单的SQL语句建议直接通过注解的方式配置SQL语句&#xff1a; Delete("delete from user where id#{id}") Integer deleteById(Integer id);但是…

git使用-小白入门2

git使用-小白入门2 分支git branch——显示分支git checkout -b——创建&#xff0c;切换分支git merge——合并分支git log --graph——以图标形式查看分支 推送至远程仓库 分支 在进行多个并行作业时&#xff0c;我们会用到分支。在这类并行开发的过程中&#xff0c;往往同时…

OpenAI Whisper:语音识别技术的革新者—深入架构与参数

当下语音识别技术正以前所未有的速度发展&#xff0c;极大地推动了人机交互的便利性和效率。OpenAI的Whisper系统无疑是这一领域的佼佼者&#xff0c;它凭借其卓越的性能、广泛的适用性和创新的技术架构&#xff0c;正在重新定义语音转文本技术的规则。今天我们一起了解一下Whi…

TiDB常见操作指南:从入门到进阶

TiDB常见操作指南&#xff1a;从入门到进阶 TiDB作为一个分布式数据库&#xff0c;提供了丰富的操作接口和功能。无论是基本的数据库管理&#xff0c;还是更为复杂的分布式事务处理&#xff0c;TiDB都能灵活应对。在这篇文章中&#xff0c;我们将总结几种TiDB常见操作&#xf…

NVIDIA CUDA Linux 官方安装指南

本文翻译自&#xff1a;https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#post-installation-actions NVIDIA CUDALinux安装指南 CUDA工具包的Linux安装说明。 文章目录 1.导言1.1.系统要求1.2.操作系统支持政策1.3.主机编译器支持政策1.3.1.支持的C方言…