【问题标题】:Issues when calling to a function, C++调用函数时的问题,C++
【发布时间】:2021-05-02 20:54:27
【问题描述】:

我需要帮助。

我编写了一个代码,目的是修改 .txt 中特定位置的值。我的第一个想法是将我所有的 .txt 存储在一个向量中,然后处理该向量。

为此我创建了2个函数:一个基本上是在.txt中分割一行,另一个是在txt中找到好的行,找到后通过调用第一个分割行然后我可以修改想要的值。

或多或少,您可以阅读以下代码:

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

using namespace std;

vector<string> split(string myData) {
    vector<string> temporary;
    string delim = " ";
    string buf= "";
    int i = 0;
    while (i < myData.length()) {
        if (myData[i] != delim)
            buf += myData[i];
        else if (myData[i] == delim) {
            if (buf != ""){temporary.push_back(buf);}
            buf = "";
        } 
        i++;
    }
    return temporary;
}

void test (){
    fstream yourfile;
    string line, someString;
    vector<string> fichier;
    someString = "EnergySampler";
    ofstream monflux("/Users/geant4_simulation_20210413/results/myFile1MeV.txt");
    ifstream myFile("benchmark.txt");
    double number;
    
//////STORING THE .TXT IN VECTOR    
    
    if (myFile){
        while (getline(myFile,line)){
            fichier.push_back(line);
        } //end while
    } //end if
    else cerr << "Your file couldn't be opened";
     
////LOOKING FOR THE LINE AND MODIFY IT    

    for (int i(0); i<fichier.size(); i++) {
        size_t found = fichier[i].find(someString);
        if(found != string::npos){ // si existe
            //cout << fichier[i] << endl;
            vector<string> splitted ;
            splitted=split(fichier[i]);      // problem here...
            cout << splitted.size() <<endl;
            
            
            for (int k = 0; k < splitted.size(); k++){
                //number = strtod(splitted[k],&end);
                cout << splitted[i] << endl;
            }
            
            
        } //end if
    }
    
    myFile.close();
}

当我独立测试拆分函数时,它运行良好,它返回为向量中的每个字符串存储的行。

例如:split("$ DOUBLE EnergySampler 0.019 $")

返回

(std::vector&lt;std::string&gt;) { "$", "DOUBLE", "EnergySampler", "0.019" }

到目前为止一切顺利!我只想选择“0.019”。

但是在 test() 函数中调用函数时出现问题,我使用的向量:splitted=split(fichier[i]); 似乎只有 2 个值,而您可以在上面看到我的行包含 4 个。

然后我的问题是:为什么split() 在独立调用时起作用,而不是在从函数内部调用时起作用?

谢谢你。

PS : 我正在使用 C++ 解释器 ROOT 编译我的函数。

【问题讨论】:

  • 您确定benchmark.txt 中的delim 都是空格吗?他们也可能是TABs (\t) 不是吗?
  • 是的,我检查了它占用了所有空格,并且我的文本文件可以正常工作。谢谢。

标签: string vector split


【解决方案1】:

虽然代码看起来还不错,但我发现它有两个错误。

  1. 我不确定这是否是故意的,但split 函数永远不会返回最后一个匹配项。在您的示例中,调用split("$ DOUBLE EnergySampler 0.019 $") 将返回向量["$", "DOUBLE", "EnergySampler", "0.019"]。请注意向量末尾缺少的"$"。因此,要解决此问题,您需要检查 buf 在函数末尾是否不为空,如下所示(另外,看起来 ROOT 解释器接受将 charstring 进行比较,但我正确定义了 @ 987654329@ as char 在修复):

    vector<string> split(string myData) {
        vector<string> temporary;
        char delim = ' ';
        string buf= "";
        int i = 0;
        while (i < myData.length()) {
            if (myData[i] != delim)
                buf += myData[i];
            else if (myData[i] == delim) {
                if (buf != ""){temporary.push_back(buf);}
                buf = "";
            }
            i++;
        }
        if (buf != ""){temporary.push_back(buf);}
        return temporary;
    }
    
  2. 其次,当您迭代splitted 向量时,您使用的是i 索引变量(来自外部for),而不是来自内部fork 索引变量。如果我像下面这样修复它,它会起作用:

            for (int k = 0; k < splitted.size(); k++){
                //number = strtod(splitted[k],&end);
                cout << splitted[k] << endl;
            }
    

所以,为下面的benchmark.txt 运行它:

$ DOUBLE EnergySampler 0.019 $
$ DOUBLE Dummy 0.123 $
$ DOUBLE EnergySampler 0.018 $
$ DOUBLE Dummy 0.321 $
$ DOUBLE EnergySampler 0.017 $

它将输出以下内容:

5
$
DOUBLE
EnergySampler
0.019
$
5
$
DOUBLE
EnergySampler
0.018
$
5
$
DOUBLE
EnergySampler
0.017
$

【讨论】:

  • 好的,谢谢你的详细回答,我明白了!现在我可以做我想做的了。
猜你喜欢
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 2011-08-16
  • 2017-07-17
相关资源
最近更新 更多