字符串分割
字符串截取
0、find函数
参考:https://blog.youkuaiyun.com/haiross/article/details/45746203
find函数
原型:
size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出现的位置。
参数说明:str为子字符串,pos为初始查找位置。
返回值:找到的话返回第一次出现的位置,否则返回string::npos
1、使用std::string::substr()函数截取。
string substr (size_t pos = 0, size_t len = npos) const;
功能:按照条件截取字符串
参数:pos=截取起始位
其中strSub是需要寻找的子字符串,npos为查找起始位置。找到返回子字符串首次出现的位置,否则返回-1;
len=截取长度
2、cut=“jpg”,得到扩展名。其中,str.find_last_of(".")返回str字符串中最后一个’.'的所在下标,这里返回8(int)。关于string::find_first_of()、string::find_first_not_of()、string::find_last_of()、string::find_last_not_of()
string str1 = "cup,car,person,car,booo";
string str2 = "ako";
int num_1 = str1.find_first_of(str2);//返回str1中第一个与str2的第一个字符('a')相同字符的下标 ,返回5
int num_2 = str1.find_first_not_of(str2);//返回str1中第一个与str2的第一个字符('a')不同字符的下标 ,返回0
int num_3 = str1.find_last_of(str2);//返回str1中最后一个与str2的最后一个字符('o')相同字符的下标 ,返回22
int num_4 = str1.find_last_not_of(str2);//返回str1中最后一个与str2的最后一个字符('o')不同字符的下标 ,返回19
所以,截取拓展名的代码如下:
string str = "image007.jpg";
string cut= str.substr(0,str.find_last_of("."));
3、strtok函数
原型: char *strtok(char *str, const char *delim);
功能: 分解字符串为一组字符串。
参数说明:str为要分解的字符串,delim为分隔符字符串。
返回值: 从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。
其它: strtok函数线程不安全,可以使用strtok_r替代。
//借助strtok实现split
#include <string.h>
#include <stdio.h>
int main()
{
char chArry[] = "one two three,four * five";
const char *d = " ,*";
char *p;
p = strtok(s,d);
while(p != NULL)
{
printf("%s\n", p);
p=strtok(NULL, d);
}
return 0;
}
利用字符串做数组下标索引,做字符统计。
//统计每个字符串出现的次数
int main(){
string input;
while(getline(cin, input)){
vector<int> table(125,0);
for(auto &ele: input){
table[ele] ++;
}
for(int i = 0;i < table.size();i++){
if(table[i] != 0){
printf("%c %d ",i,table[i]);
}
}
cout<<endl;
}
return 0;
}
erase的使用
有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
容器中元素查找
find可以找到目标元素的迭代器,找不到返回end(),如果寻找的对象是结构体,则需要重载==。
vector <int>::iterator iElement = find(vecIntegerArray.begin(),
vecIntegerArray.end(),3);
单链表的创建与使用
参考:https://blog.youkuaiyun.com/chixujohnny/article/details/50900504
注意:头指针需要单独进行初始化并记录,但是释放内存不要释放两次。
#include <iostream>
using namespace std;
/* 创建一个单链表 */
struct ListNode{
int m_key;
ListNode* next;
};
void createList(ListNode* pHead){
ListNode* p = pHead;
for (int i = 1; i < 10; ++i) {
ListNode* pNewNode = new ListNode;
pNewNode->m_key = i; // 将新节点的值赋值为i
pNewNode->next = NULL;
p->next = pNewNode; // 上一个节点指向这个新建立的节点
p = pNewNode; // p节点指向这个新的节点
}
}
int main(){
ListNode* head = NULL;
head = new ListNode;
head->m_key = 0;
head->next = NULL;
createList(head);
//释放指针
while(head != NULL){
ListNode* temp = head;
head = head->next;
delete temp;
}
return 0;
}
eumn与int的相互转换
enum Color { red, white, blue};
- enum转int
void f()
{
Color x = red;
Color y = white;
Color z = blue;
int n;
n = x; // change n to 0
n = y; // change n to 1
n = z; // change n to 2
}
- int转enum
void f()
{
Color x;
x = red; // change x to red
x = Color(1); // change x to white
x = Color(2); // change x to blue
x = 2; // compile-time error: can't convert int to Color
}
pair的使用
参考:https://blog.youkuaiyun.com/sevenjoin/article/details/81937695
主要用法:
pair<int, double> p1;
p1 = make_pair(1, 1.2);
cout << p1.first << p1.second << endl;
//output: 1 1.2
int a = 8;
string m = "James";
pair<int, string> newone;
newone = make_pair(a, m);
通过tie获得元素值:
std::pair<std::string, int> getPreson() {
return std::make_pair("Sven", 25);
}
int main(int argc, char **argv) {
std::string name;
int ages;
std::tie(name, ages) = getPreson();
std::cout << "name: " << name << ", ages: " << ages << std::endl;
return 0;
}
数据类型的转换
使用stringstream进行所有数据类型的转换,需要#include 。注意在清空缓冲区时,不应该去调用clear。
具体用法如下:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
char nzArr[10] = "aaa";
string str1 = nzArr;
int nNum = 1111;
string str2("12234");
stringstream strStream(str2);//实例化对象并且初始化
strStream.str(""); //使用str()清空缓冲的内存
strStream << nNum;
strStream >> nzArr;
strStream >> str1;
return 0;
}
笔试必掌握的algorithms头文件
- 参考:https://blog.youkuaiyun.com/FX677588/article/details/53007038
- “algorithm”头文件是一个高效而方便的工具包,里面包含的基本数据结构和基本算法能够大大提高我们编程效率。诸如排序,字典全排序,查找字符,反转字符串等等算法不需要我们自行定义和编程,直接调用该头文件很方便。
- 所有算法接口的介绍:https://blog.youkuaiyun.com/fengbingchun/article/details/78034969
技巧总结
参考:https://blog.youkuaiyun.com/qq_38410730/article/details/80943729
1、“去重”与“排序”
例如:输入11 10 20 40 32 67 40 20 89 300 400 15,输出10 15 20 32 40 67 89 300 400。
总结:将输入的数字或者字符串直接作为数组下标,自动的排序计数。
#include <iostream>
using namespace std;
int main() {
int N, n;
while (cin >> N) {
int a[1001] = { 0 };
while (N--) {
cin >> n;
a[n] = 1;
}
for (int i = 0; i < 1001; i++)
if (a[i])
cout << i << endl;
}
return 0;
}
2、输入与输出
int a;
while(cin >>hex>> a){
cout<<oct<<a<<endl;
}
3、二进制数的转换
//求int型正整数1的个数
int input;
cin>> input;
int output = 0;
//求出所有的二进制数
while(input >0){
//通过取余数,得到二进制的位数
int bit = input %2;
input = input/2;
if(bit){
output++;
}
}
cout<<output<<endl;