You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C++将文本文件中的+、-替换为plus和minus

C++字符替换功能实现方案

核心实现思路

新增一个字符串替换工具函数,对读取到的每个字段内容做字符替换,再存入容器即可。

新增替换函数

在main函数之前加入如下通用替换函数:

string replace_symbols(string s) {
    string res;
    for (char c : s) {
        if (c == '+') {
            res += "plus";
        } else if (c == '-') {
            res += "minus";
        } else {
            res += c;
        }
    }
    return res;
}

修改原代码的读取逻辑

每次读取到字段内容后,先调用替换函数再存入separated_fields:

// 读取ID后先替换再存入
getline(ss,ID,',');
separated_fields.push_back(replace_symbols(ID));

// fname同理
getline(ss,fname,',');
separated_fields.push_back(replace_symbols(fname));

// lname同理
getline(ss,lname,',');
separated_fields.push_back(replace_symbols(lname));

// course循环读取时也执行替换
while( getline(ss,course,',') ) {
    separated_fields.push_back(replace_symbols(course));
}

修正原代码的遍历逻辑

原代码内层循环用table[0].size()存在问题,不同行的字段数量可能不一致,要改成取当前行的长度:

for(int i=0 ;i<table.size();i++){
    // 把table[0].size()改成table[i].size()适配不同长度的行
    for(int j=0 ;j<table[i].size();j++){
        cout<<table[i][j]<<"\t\t";
        // 需要写入输出文件的话加下面这句
        // outFile<<table[i][j];
        // if(j != table[i].size()-1) outFile<<",";
    }
    cout<<endl;
    // 需要写入输出文件的话加下面这句
    // outFile<<endl;
}

完整修改后代码

#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

string replace_symbols(string s) {
    string res;
    for (char c : s) {
        if (c == '+') {
            res += "plus";
        } else if (c == '-') {
            res += "minus";
        } else {
            res += c;
        }
    }
    return res;
}

int main()
{
    ifstream inFile("registration.txt");
    ofstream outFile("replaced.txt");

    vector<vector<string> > table;
    if (inFile.is_open())
    {
        string line;
        while( getline(inFile,line) )
        {
            stringstream ss(line);
            vector<string> separated_fields;
            cout<<line<<endl;
            cout<<"----"<<endl;

            string ID, fname, lname;
            getline(ss,ID,',');
            separated_fields.push_back(replace_symbols(ID));
            getline(ss,fname,',');
            separated_fields.push_back(replace_symbols(fname));
            getline(ss,lname,',');
            separated_fields.push_back(replace_symbols(lname));

            string course;
            while( getline(ss,course,',') )
            {
                separated_fields.push_back(replace_symbols(course));
            }
            cout<<separated_fields[0];
            cout<<"\n";

            table.push_back(separated_fields);
        }
        inFile.close();
    }
    cout<<"------------------"<<endl;

    for(int i=0 ;i<table.size();i++){
        for(int j=0 ;j<table[i].size();j++){
            cout<<table[i][j]<<"\t\t";
            outFile<<table[i][j];
            if(j != table[i].size()-1) outFile<<",";
        }
        cout<<endl;
        outFile<<endl;
    }
    outFile.close();
    return 0;
}

内容的提问来源于stack exchange,提问作者Nour Hany

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 15:36:03