C++ written test processing complex input and output

1 Enter multiple lines of strings separated by certain symbols

For example, the title requires the input of n lines of strings, and there are several words separated by certain symbols in each line of strings.
Enter 1:

3
hello apple set bad
sort delete new
auto map key

Enter 2:

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

Generally, this kind of input needs to extract elements and put them in the container when solving the problem, so that the next step can be processed. How to extract it? The code is as follows
Case 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<<" ";
    }
}

Case 2: Need to understand getline usage

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 usage of strtok and strsub

Case 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;
}

Case 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);//默认到结尾
}

Guess you like

Origin blog.csdn.net/weixin_42907822/article/details/126515153