7.C++中string的一些超常用函数 (附习题)

目录

1.find

2.atoi

3.to_string

4.getline

【leetcode 习题】

387.字符串中的第一个唯一字符

125. 验证回文串


1.find

1.查找第一次出现的目标字符串说明:如果查找成功则输出查找到的第一个位置,否则返回-1;

s1.find(s2)

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s1 = "abcdef";
    string s2 = "de";
    int ans = s1.find(s2) ;   //在S1中查找子串S2
    cout << ans << endl;
    system ("pause");
}

2.查找从指定位置开始的第一次出现的目标字符串

s1.find(s2, 2) 

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s1 = "abcdef";
    string s2 = "de";
    int ans = s1.find(s2, 2) ;   //从S1的第二个字符开始查找子串S2
    cout << ans << endl;
    system("pause");
}

2.atoi

atoi()函数的语法非常简单

int atoi (const char * str);
  • int integer type which is the type of returned value.

    int整数类型,它是返回值的类型。

  • const char * is a constant char array that is equal to a string whose variable name is str.

    const char * 表示传入一个指向常量字符的指针,名为str

作用:在C和C ++中将字符串/字符转换为整数 (Convert String/Char To Integer In C and C++)

可以参考如下实例:

#include <iostream>
#include <cstdlib>
 
int main()
{
    const char *str1 = "57";
    const char *str2 = "314.159";
    const char *str3 = "52345 some text";
    const char *str4 = "some text 25";
 
    int mynum1 = std::atoi(str1);
    int mynum2 = std::atoi(str2);
    int mynum3 = std::atoi(str3);
    int mynum4 = std::atoi(str4);
 
    std::cout << "atoi(\"" << str1 << "\") is " << mynum1 << '\n';
    std::cout << "atoi(\"" << str2 << "\") is " << mynum2 << '\n';
    std::cout << "atoi(\"" << str3 << "\") is " << mynum3 << '\n';
    std::cout << "atoi(\"" << str4 << "\") is " << mynum4 << '\n';
}

  • If given string or char array is floating-point like “314.159”  in only integer part will be converted where the result will be “314”

    如果给定的字符串或char数组是浮点数,例如“ 314.159”,则仅整数部分将被转换,结果为“ 314”
  • If there are some non-numerical chars in the given char array or string they will not convert and there will be no error where only the integer part will be converted. As an example “52345 some text” will be converted 52345

    如果给定的char数组或字符串中有一些非数字char,它们将不会转换,并且只有整数部分会被转换也不会出错。 例如,“ 52345 some text”将被转换为52345
  • If the start of the char array or string and there are numerical characters after them this will be converted into 0 as an integer value. As an example “some text 25” will be converted into 0.

    如果char数组或字符串的开头,并且后面有数字字符,则将其转换为0作为整数值。 例如,“一些文本25”将转换为0。
  • If the numerical characters are between non-numeric characters the conversion result will be 0 too.

    如果数字字符介于非数字字符之间,则转换结果也将为0。
  • If the char array or string starts with numeric character and after that, there is a non-numeric, and then numeric character only the starting numeric character will be converted. As an example “25 text 50” will be converted into 25.

    如果char数组或字符串以数字字符开头,并且之后是非数字字符,仅将起始数字字符转换为数字字符。 例如,“ 25个文本50”将转换为25。

【总结来说】:字符转化为数字,是以起始为主要参考的

3.to_string

数字常量转换为字符串,返回值为转换完毕的字符串

头文件:#include < string >(必须加)

string s = to_string(i); //将整数i转换为字符串表示形式

附:如果是string s = to_string(‘3’),则结果是s = 51。
需要对s = s -48; 才能得到正确的结果。

#include <iostream>   
#include <string>     
using namespace std;
 
int main()
{
    string pi = "pi is " + std::to_string(3.1415926);
    string perfect = to_string(1 + 2 + 4 + 7 + 14) + " is a perfect number";
    cout << pi << '\n';
    cout << perfect << '\n';
    system("pause");
    return 0;
}

4.getline

作用:打印一行字符串(可打印空格)

应用:

题解: 

【leetcode 习题】

387.字符串中的第一个唯一字符

!!!  get以坐标为方式的查找次数加法  count [ s [ i ] ]+=1

125. 验证回文串

如果有兴趣了解原理,可以读一下下面这篇文章,来更深入的了解string:

STL 的string类怎么啦

  • 82
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 50
    评论
好的,以下是一些 C++ 的递归函数习题: 1. 编写一个递归函数,计算 n 的阶乘 ```cpp int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } ``` 2. 编写一个递归函数,计算斐波那契数列的第 n 项 ```cpp int fibonacci(int n) { if (n == 0 || n == 1) { return n; } else { return fibonacci(n - 1) + fibonacci(n - 2); } } ``` 3. 编写一个递归函数,计算一个数字的二进制表示 1 的个数 ```cpp int countOnes(int n) { if (n == 0) { return 0; } else { return (n % 2 == 1) + countOnes(n / 2); } } ``` 4. 编写一个递归函数,计算一个数字的反转 ```cpp int reverse(int n) { if (n < 10) { return n; } else { return (n % 10) * pow(10, floor(log10(n))) + reverse(n / 10); } } ``` 5. 编写一个递归函数,计算一个字符串的长度 ```cpp int stringLength(string str) { if (str == "") { return 0; } else { return 1 + stringLength(str.substr(1)); } } ``` 6. 编写一个递归函数,判断一个字符串是否是回文字符串 ```cpp bool isPalindrome(string str) { if (str.length() <= 1) { return true; } else if (str[0] != str[str.length() - 1]) { return false; } else { return isPalindrome(str.substr(1, str.length() - 2)); } } ``` 7. 编写一个递归函数,计算一个数组所有元素的和 ```cpp int arraySum(int arr[], int size) { if (size == 0) { return 0; } else { return arr[0] + arraySum(arr + 1, size - 1); } } ``` 8. 编写一个递归函数,计算一个数组所有元素的平均值 ```cpp double arrayAverage(int arr[], int size) { if (size == 0) { return 0; } else { return (arr[0] + (size - 1) * arrayAverage(arr + 1, size - 1)) / size; } } ``` 9. 编写一个递归函数,计算一个数组的最大值 ```cpp int arrayMax(int arr[], int size) { if (size == 1) { return arr[0]; } else { return max(arr[0], arrayMax(arr + 1, size - 1)); } } ``` 10. 编写一个递归函数,计算一个数组的最小值 ```cpp int arrayMin(int arr[], int size) { if (size == 1) { return arr[0]; } else { return min(arr[0], arrayMin(arr + 1, size - 1)); } } ``` 希望这些练习题能够帮助你练习 C++ 的递归函数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 50
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值