欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

算法笔记 C++ STL学习(string的常见用法)

程序员文章站 2022-07-08 16:43:21
...

string:存放字符串
字符串的访问:
单个字符:字符数组访问方式
整个字符串:只能用cin,cout

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<stdlib.h>
    #include<string>
    using namespace std;
    int main(void){
        string str = "fdsfdsf";
        int i;
        for(i=0;i<str.length();i++){
            printf("%c",str[i]);
        }
        printf("\n");
        cout<<str;
        return 0;
    }

用printf输出string:使用c_str()将string转化为字符数组

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<stdlib.h>
    #include<string>
    using namespace std;
    int main(void){
        string str = "fdsfdsf";
        printf("%s",str.c_str());
    }

通过迭代器访问,与其他迭代器用法相同

	#include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<stdlib.h>
    #include<string>
    using namespace std;
	int main(void){
        string str = "fdsfdsf";
        string::iterator it;
    }

常用函数:
字符串拼接

    #include<stdio.h>
    #include<iostream>
    #include<string>
    using namespace std;
    int main(void){
        string str1 = "abc",str2 = "def",str3;
        str3 = str1 + str2;
        str1 += str2;
        cout<<str1;
        cout<<"\n";
        cout<<str3;
    }

字符串比较大小:(==,>=,<)
字符串的长度:size(),length()
字符串插入:
insert(pos,string):在pos位置插入string

    #include<stdio.h>
    #include<iostream>
    #include<string>
    using namespace std;
    int main(void){
        string str1 = "abc",str2 = "def",str3;
        str1.insert(2,str2);
        cout<<str1;
    }

insert(it,it2,it3):it为字符串的欲插入位置,it2和it3为待插字符串的首尾迭代器

    #include<stdio.h>
    #include<iostream>
    #include<string>
    using namespace std;
    int main(void){
        string str1 = "abc",str2 = "def",str3;
        str1.insert(str1.begin()+2,str2.begin(),str2.end());
        cout<<str1;
    }

erase():删除元素
clear():清空
substr():返回从pos位开始,长度为len的子串

    #include<stdio.h>
    #include<iostream>
    #include<string>
    using namespace std;
    int main(void){
        string str1 = "abcdef",str2 = "def",str3;
        cout<<str1.substr(2,3);
    }

常数:string::npos find函数失配时的返回值,值为 -1 或4294967295
find(str):返回子串第一次出现的位置
find(str,pos):从pos位开始查找,返回子串第一次出现的位置

    #include<stdio.h>
    #include<iostream>
    #include<string>
    using namespace std;
    int main(void){
        string str1 = "Thank you for your smile";
        string str2 = "you";
        string str3 = "me";
        cout<<str1.find(str2);
        cout<<str1.find(str3);
        return 0;
    }

replace(pos,len,str2):从pos位开始长度为len的字符串替换为str2
replace(it1,it2,str2):[it1,it2)范围内的字符串替换为str2

    #include<stdio.h>
    #include<iostream>
    #include<string>
    using namespace std;
    int main(void){
        string str1 = "Thank you for your smile";
        string str2 = "you";
        string str3 = "me";
        str1.replace(2,4,str2);
        str1.replace(str1.begin(),str1.end(),str2);
        cout<<str1<<endl;
        return 0;
    }

1060 Are They Equal (25分)
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×10​5 with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.

Input Specification:
Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10​100​​, and that its total digit number is less than 100.

Output Specification:
For each test case, print in a line YESif the two numbers are treated equal, and then the number in the standard form 0.d[1]...d[N]*10^k(d[1]>0unless the number is 0); or NOif they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.

Note: Simple chopping is assumed without rounding.

Sample Input 1:

3 12300 12358.9

Sample Output 1:

YES 0.123*10^5

Sample Input 2:

3 120 128

Sample Output 2:

NO 0.120*10^3 0.128*10^3

Code:

#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int n;
string deal(string s,int &e){
    int k = 0;
    while(s.length() > 0 && s[0] == '0'){
        s.erase(s.begin());
    }
    if(s[0] == '.'){
        s.erase(s.begin());
        while(s.length() > 0 && s[0] == '0'){
            s.erase(s.begin());
            e--;
        }
    }
    else{
        while(k<s.length() && s[k] != '.'){
            k++;
            e++;
        }
        if(k < s.length()){
            s.erase(s.begin() + k);
        }
    }
    if(s.length() == 0){
        e = 0;
    }
    int num = 0;
    k = 0;
    string res;
    while(num < n){
        if(k < s.length()){
            res += s[k++];
        }
        else{
            res += '0';
        }
        num++;
    }
    return res;
}
int main(void){
    string str1,str2,str3,str4;
    cin>>n>>str1>>str2;
    int e1 = 0;
    int e2 = 0;
    str3 = deal(str1,e1);
    str4 = deal(str2,e2);
    if(str3 == str4 && e1 == e2){
        cout<<"YES 0."<<str3<<"*10^"<<e1<<endl;
    }
    else{
        cout<<"NO 0."<<str3<<"*10^"<<e1<<" 0."<<str4<<"*10^"<<e2;
    }
    return 0;
}

算法笔记 C++ STL学习(string的常见用法)

相关标签: 算法笔记