C++|文字列の分割

C++ には文字列を分割するための独自の関数がない
ため、自分で実装します。

分割要件:

入力:
1,24,569,78954,0,9,1,1111,230
これらの数値を出力します

実装コード:
#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;
}

入力:
1,24,569,78954,0,9,1,1111,230
出力:
1
24
569
78954
0
9
1
1111
230

拡大する:


「1:23,7:789,8:34,9:56,6:123,2:396」と入力すると
、これはキーと値のペアのセットです。各ペアはコロンで区切られ、各ペアは次のようになります。カンマで区切る
. 数値の各ペアを並べ替えた結果を、各ペアの最初の数値で小さい値から大きい値の順に出力します。

実装コード:
#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;
}

補足:
以下の入力の場合、交換操作のみが実行できる必要があります シーケンスをソートするのに必要な最小交換回数を求めてください
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;
}

おすすめ

転載: blog.csdn.net/holly_Z_P_F/article/details/132644348