C++|Split string

C++ does not have its own function for splitting strings,
so implement one yourself.

Split requirements:

Input:
1,24,569,78954,0,9,1,1111,230Output
these numbers

Implementation code:
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
vector<string> splitString(string str,char split){
    
     //分割字符串 返回字符串数组
    vector<string> res;
    string s = "";
    for (int i = 0; i < str.length();i++){
    
    
        if(str[i]==split){
    
    //遇到分隔符
            res.push_back(s);
            s = "";
        }else if(i==str.length()-1){
    
    //遇到结尾
            s += str[i];
            res.push_back(s);
            s = "";
            break;
        }
        else{
    
    
            s += str[i];
        }
    }
    return res;
}
int main(){
    
    
    string str;
    vector<int> res;
    cin >> str;
    vector<string> input = splitString(str,',');//确定分割符
    for (int i = 0; i < input.size();i++)
    {
    
    
        //cout << input[i] << endl; //输出分割的字符串
        int x = stoi(input[i]); //字符串转化为十进制数字 头文件string
        cout << x << endl;//输出数字
    }
    return 0;
}

Input:
1,24,569,78954,0,9,1,1111,230
Output:
1
24
569
78954
0
9
1
1111
230

expand:

If you enter:
1:23,7:789,8:34,9:56,6:123,2:396,
this is a set of key-value pairs. Each pair is separated by a colon, and each pair is separated by a comma
. Output the results of sorting each pair of numbers, sorted by the first number in each pair from small to large.

Implementation code:
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
vector<string> splitString(string str,char split){
    
     //分割字符串 返回字符串数组
    vector<string> res;
    string s = "";
    for (int i = 0; i < str.length();i++){
    
    
        if(str[i]==split){
    
    //遇到分隔符
            res.push_back(s);
            s = "";
        }else if(i==str.length()-1){
    
    //遇到结尾
            s += str[i];
            res.push_back(s);
            s = "";
            break;
        }
        else{
    
    
            s += str[i];
        }
    }
    return res;
}
struct mypair{
    
    
    int a;
    int b;
};
int main(){
    
    
    string str;
    cin >> str;
    vector<mypair> res;
    vector<string> input = splitString(str,',');
    for (int i = 0; i < input.size();i++)
    {
    
    

        vector<string> p = splitString(input[i], ':');
        struct mypair x;
        x.a = stoi(p[0]);
        x.b = stoi(p[1]);
        res.push_back(x);
    }
    auto cmp = [](struct mypair x, struct mypair y){
    
    
        return x.a < y.a;
    };

    sort(res.begin(), res.end(), cmp);//排序
    for (auto i : res){
    
    
        cout << i.a << ":" << i.b << endl;
    }
    return 0;
}

Supplement:
If the input is as follows, it is required that only exchange operations can be performed. Find the minimum number of exchanges required to sort a sequence
2
A7 A2 A3 A4 A5 A6 A1 A2
A1 A3 A6 A5 A4 A7

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
vector<string> splitString(string str,char split){
    
     
    vector<string> res;
    string s = "";
    for (int i = 0; i < str.length();i++){
    
    
        if(str[i]==split){
    
    
            res.push_back(s);
            s = "";
        }else if(i==str.length()-1){
    
    
            s += str[i];
            res.push_back(s);
            s = "";
            break;
        }
        else{
    
    
            s += str[i];
        }
    }
    return res;
}
int getMinSwaps(vector<int> &nums){
    
    
    map<int, int>mp;    
    vector<int> sortedNums(nums);
    sort(sortedNums.begin(), sortedNums.end());
    for (int i = 0; i < sortedNums.size(); i++)mp[sortedNums[i]] = i ;

    int cnt = 0;
    for (int i = 0; i < nums.size();i++)
    {
    
    
        if (nums[i] == sortedNums[i])continue;
        swap(nums[i], nums[mp[nums[i]]]);
        cnt++;
    }
    return cnt;
}
int main() {
    
    
    int n;
    cin >> n;
    getchar();//吞掉后面的换行符
    while(n--){
    
    
        string str;
        vector<int> a;
        getline(cin, str);//有空格的时候 需要用getline读入
        vector<string> res = splitString(str, ' ');
        for (int i = 0; i < res.size();i++){
    
    
            string temp = res[i];
            string s = "";
            for (int j = 1; j < temp.length();j++)
                s += temp[j];
            int x = stoi(s);
            a.push_back(x);
        }
        cout << getMinSwaps(a) << endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/holly_Z_P_F/article/details/132644348