Entrada y salida complejas de procesamiento de pruebas escritas en C++

1 Ingrese múltiples líneas de cadenas separadas por ciertos símbolos

Por ejemplo, el título requiere la entrada de n líneas de cadenas y hay varias palabras separadas por ciertos símbolos en cada línea de cadenas.
Introduzca 1:

3
hello apple set bad
sort delete new
auto map key

Introduzca 2:

2
2,3,4,5,6
12,4,3,6,7

Generalmente, este tipo de entrada necesita extraer elementos y colocarlos en el contenedor al resolver el problema, para que se pueda procesar el siguiente paso. ¿Cómo extraerlo? El código es el siguiente
Caso 1:

void test1()
{
    
    
    //输入n行字符串,每行字符串内有若干个以空格间隔的单词。
    int n;
    cin>>n;
    getchar();
    //如果下面要调用getline()函数的话,这一步很关键,因为输入完n要按回车键
    //如果不把回车键\n符号用getchar()函数读取走的话,那么接下来的getline()函数不管你有没有输入都会直接读取换行符号
    set<string> words;
    for(int i=0;i<n;i++){
    
    
        string s;
        getline(cin,s);
        stringstream ss(s);//把s拷贝给ss,ss可以像输入输出一样使用
        string tmp;
        while(ss>>tmp){
    
    
            words.insert(tmp);
        }
    }

    for(string w:words){
    
    
        cout<<w<<" ";
    }
}

Caso 2: Necesidad de comprender el uso de getline

void test2()
{
    
    
    //输入n行字符串,每行字符串内有若干个以,间隔的单词。
    int n;
    cin>>n;
    getchar();
    set<string> words;
    for(int i=0; i<n; i++){
    
    
        string s;
        getline(cin, s);
        stringstream ss(s);
        string w;
        while(getline(ss,w,',')){
    
    
            words.insert(w);
        }
    }

    for(string w:words){
    
    
        cout<<w<<" ";
    }
}
#include "bits/stdc++.h"
using namespace std;

int main()
{
    
    
    //test1();
    test2();
    return 0;
}

2 uso de strtok y strsub

Caso 1:

输入:
2
app,happy;new look
bug gap,del;key
输出:
app happy new look bug gap del key
#include "bits/stdc++.h"

using namespace std;

string char2string(char* src){
    
    
    string res;
    while(*src != '\0'){
    
    
        res += *src;
        src++;
    }
    return res;
}
void test3()
{
    
    
    int n;
    cin >> n;
    getchar();
    set<string> words;
    for(int i=0;i<n;i++){
    
    
        string s;
        getline(cin,s);
        char* tmp = strtok((char*)s.c_str(),", ;");
        //strtok函数,第一次使用时,第一个参数要传char*
        //s.c_str() 类型是const char* 需要强转
        //函数在处理过程中,需要分割符就把分割符变成'\0'
        string w;
        if(tmp){
    
    
            w = char2string(tmp);//把从开始到分割符'\0'之间的字符形成字符串
            words.insert(w);
        } 
        
        while(tmp){
    
    
            tmp = strtok(nullptr,", ;");//如果继续分割剩下的字符,第一个参数传入nullptr
            if(tmp){
    
    
                w = char2string(tmp);//把从开始到分割符'\0'之间的字符形成字符串
                words.insert(w);
            } 
        }
    }

    for(string w:words){
    
    
        cout<<w<<" ";
    }

}
int main()
{
    
    
    test3();
    return 0;
}

Caso 2:

实现字符串分割函数
输入:
string s = "key:value";
输出:
key 
value

void curString(const string& target, const string& split,string& out1,string& out2)
{
    
    
    size_t pos = target.find(split);
    out1 = target.substr(0,pos);
    out2 = target.substr(pos+1);//默认到结尾
}

Supongo que te gusta

Origin blog.csdn.net/weixin_42907822/article/details/126515153
Recomendado
Clasificación