7.STL中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 *是一个常量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类怎么啦

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

linux安装Openresty

安装必要的依赖库 指定仓库地址 下载openresty 添加环境变量 vi /etc/profile i export NGINX_HOME/usr/local/openresty/nginx/ export PATH${NGINX_HOME}/sbin:$PATH esc :wq source /etc/profile #启动 nginx # 重启 nginx -s reload #关闭 nginx -s stop

2024年低碳发展与新能源技术国际学术会议(ICLCDNET 2024)

2024年低碳发展与新能源技术国际学术会议&#xff08;ICLCDNET 2024) 2024 International Conference on Low Carbon Development and New Energy Technologies 一、【会议简介】 随着全球气候变化的日益严峻&#xff0c;低碳发展和新能源技术已成为国际社会共同关注的焦点。在…

python怎么读取xml

引入XML组件&#xff1a;import xml.dom.minidom。 创建一个xml文件&#xff0c;<?xml version"1.0" encoding"utf-8"?>。 加载读取XML文件&#xff0c;xml.dom.minidom.parse(abc.xml)&#xff0c;这是xml文件的对象。 获取XML文档对象&#xff0…

柯桥法语学习-5大法语听写网站:全力助攻你的dictée!

提到法语dicte&#xff0c;绝对可是法语学生们的老大难&#xff0c;简直就是心痛得不能自已啊&#xff01;所以今天&#xff0c;法语君整理了5个听写网站助攻大家的dicte哦&#xff01; Projet Voltaire 01 一个很容易让你对dicte上瘾的APP 写邮件、实习报告或者动机信时&…

数据新探:用Python挖掘互联网的隐藏宝藏

Hello&#xff0c;我是你们的阿佑&#xff0c;今天给大家上的菜是——数据存储&#xff01;听起来枯燥无味&#xff1f;错了&#xff01;阿佑将带你重新认识数据存储的艺术。就像为珍贵的艺术品寻找完美的展览馆&#xff0c;为你的数据选择合适的存储方式同样重要&#xff01; …

基准电流源电路仿真

1.补全电路 2.更改vpluse属性 3.添加tran仿真&#xff0c;因为加入启动电路主要看的就是tran仿真 4.启动仿真 5.看电路曲线 先点这个main form 不用选择ok&#xff0c;直接点中四条线&#xff0c;中间第2条曲线如果出现那样一个小波动后面没有起伏就说明成功了。

一篇文章带你入门CSRF

1.什么是CSRF 用一个形象生动的比喻就是&#xff1a; 你给朋友的房子留下了备用钥匙&#xff0c;但是有人偷偷拿走了这把钥匙&#xff0c;然后用这把钥匙进入了你朋友的房子并做了各种坏事。你朋友以为只有你能使用这把钥匙&#xff0c;所以没对任何操作产生怀疑。 在这个比…

.NET周刊【5月第2期 2024-05-12】

国内文章 C#在工业数字孪生中的开发路线实践 https://mp.weixin.qq.com/s/b_Pjt2oii0Xa_sZp_9wYWg 这篇文章探讨了C#在工业数字孪生技术中的应用&#xff0c;介绍了三种基于C#的数字孪生系统实现方案&#xff1a; WPF Unity&#xff1a;结合WPF技术和Unity引擎&#xff0c…

算法练习day8

反转字符串 代码随想录 0344.反转字符串 344. 反转字符串 - 力扣&#xff08;LeetCode&#xff09; &#xff08;用时&#xff1a;0.05小时&#xff09; 思路 这道题很简单也很经典。 字符串的反转方法有很多&#xff0c;我这里是用for循环通过数组长度和下标计算来交换。…

如何用Rust获取CPU、内存、硬盘的信息?

目录 一、用Rust获取CPU、内存、硬盘的信息 二、知识点 systemstat 一、用Rust获取CPU、内存、硬盘的信息 首先&#xff0c;需要添加systemstat库到Cargo.toml文件&#xff1a; [dependencies] systemstat "0.2.3" 在Rust代码中使用它&#xff1a; extern crat…

pip镜像源

1.1 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple 1.2 阿里云 https://mirrors.aliyun.com/pypi/simple/ 1.3 网易 https://mirrors.163.com/pypi/simple/ 1.4 豆瓣 https://pypi.douban.com/simple/ 1.5 百度云 https://mirror.baidu.com/pypi/simple/ 1.6 中科大 ht…

Android 集成Bugly完成线上的异常Exception收集及处理

文章目录 &#xff08;一&#xff09;添加产品APP&#xff08;二&#xff09;集成SDK&#xff08;三&#xff09;参数配置权限混淆 &#xff08;四&#xff09;初始化 &#xff08;一&#xff09;添加产品APP 一&#xff09;在个人头像 -> 我的头像 -> 新建产品 二&…

基于IDEA快速创建一个SpringMVC项目并且配置Tomcat

1&#xff0c;打开IDEA&#xff0c;新建Maven项目【使用web模板创建】 使用社区版的同学创建普通的maven项目&#xff0c;并配置项目的webapp&#xff0c;详情可参考 快速创建一个SpringMVC项目&#xff08;IDEA&#xff09; 2&#xff0c;在main目录下创建Java和resource目录…

原生小程序开发如何使用 tailwindcss

原生小程序开发如何使用 tailwindcss 原生小程序开发如何使用 tailwindcss 前言什么是 weapp-tailwindcss ?0. 准备环境以及小程序项目1. 安装与配置 tailwindcss 0. 使用包管理器安装 tailwindcss1. 在项目目录下创建 postcss.config.js 并注册 tailwindcss2. 配置 tailwind…

24深圳杯B题42页高质量论文+可执行代码+可视化图标(更新版)

简单麦麦https://www.jdmm.cc/file/2710640/ 比赛题目的完整版思路代码数据都会在第一时间更新上传的&#xff0c;大家可以参考我往期的资料&#xff0c;所有的资料数据以及到最后更新的参考论文都是一次付费后续免费的。注意&#xff1a;&#xff08;建议先下单占坑&#xff…

灌区信息化管理平台系统包含哪些内容?(全面介绍)

政策背景 2022年12月29日&#xff0c;水利部启动48处大中型灌区开展数字孪生灌区先行先试建设。 2023年2月24日&#xff0c;《2023年农村水利水电工作要点》:2023年农村水利水电工作的总体思路包括:紧盯保障国家粮食安全&#xff0c;加快推进大中型灌区现代化改造&#xff0c;…

1. AB路线-BFS+分层图

0AB路线 - 蓝桥云课 (lanqiao.cn) #include <bits/stdc.h> #define int long long #define endl \n using namespace std; int n,m,k; //存迷宫格子 char board[1005][1005]; //vis[i][j][k]存坐标(i,j)的格子&#xff0c;在一个周期中第k次经过它 bool vis[1005][1005][…

linux 网络管理 实验

目录 网络管理主机名管理网络管理 网络管理 主机名管理 执行如下命令查看主机名。 [rootopenEuler ~]# hostname openEuler [rootopenEuler ~]# cat /etc/hostname #这个文件是主机名的配置文件 openEuler执行如下命令临时修改主机名。 [rootopenEuler ~]# hostname huawe…

(四十二)第 6 章 树和二叉树(树的二叉链表(孩子-兄弟)存储)

1. 背景说明 2. 示例代码 1) errorRecord.h // 记录错误宏定义头文件#ifndef ERROR_RECORD_H #define ERROR_RECORD_H#include <stdio.h> #include <string.h> #include <stdint.h>// 从文件路径中提取文件名 #define FILE_NAME(X) strrchr(X, \\) ? strrch…

MySQL的msi格式安装

一、下载链接 MySQL :: Download MySQL Installer (Archived Versions) 二、安装步骤 ①选择自定义安装 ②选择要安装的产品 ③安装依赖环境 ④安装 ⑤点击下一步 ⑥配置 ⑦设置密码 ⑧命名 ⑨数据存放路径 ⑩安装配置 ①①配置环境变量 ①②验证 方法一&#xff1a; 方法二…