简体   繁体   English

使用子字符串C ++获得奇怪的输出

[英]Getting weird output with substring C++

so I am having some odd problems with what seems like a very simple problem. 所以我在看起来很简单的问题上遇到了一些奇怪的问题。

I have a vector: 我有一个向量:

vector(string) collectionOfLines; vector(string)collectionOfLines;

This holds lines of text that I got from a .txt file. 这保存了我从.txt文件中获得的文本行。 The contents of the text file are: 文本文件的内容为:

"4 “ 4

Disk 0.2 0.00005 磁盘0.2 0.00005

Mouse 0.4 0.00002 鼠标0.4 0.00002

Keyboard 0.3 0.00004 键盘0.3 0.00004

Network 0.5 0.0001" 网络0.5 0.0001“

collectionOfLines[0] = "Disk 0.2 0.00005" collectionOfLines [0] =“磁盘0.2 0.00005”

I am trying to separate this string into three different strings that are: "Disk", "0.2", and "0.00005" and then put these strings into a different vector: 我正在尝试将此字符串分成三个不同的字符串:“ Disk”,“ 0.2”和“ 0.00005”,然后将这些字符串放入不同的向量中:

vector(string) collectionOfCommands; vector(string)collectionOfCommands;

This is my loop for getting the substrings from the line string and putting them into the new vector. 这是我从循环字符串获取子字符串并将其放入新向量的循环。

string deviceName;
string interruptProbability;
string interruptTime;

for(int i = 1; i < collectionOfLines.size(); i++) { // i = 1 because I am ignoring the "4" in the txt file
    string currentLine = collectionOfLines[i];
    int index = 0;
    for(int j = 0; j < currentLine.length(); j++) {
        if(j == 0) {
            continue;
        } else if(deviceName.empty() && currentLine[j-1] == ' ') {
            deviceName = currentLine.substr(index, j-1);
            index = j;
        } else if (interruptProbability.empty() && currentLine[j-1] == ' ') {
            interruptProbability = currentLine.substr(index, j-1);
            index = j;
        } else if (!deviceName.empty() && !interruptProbability.empty()) {
            interruptTime = currentLine.substr(index, currentLine.length());
            break;
        } else {
            continue;
        }
    }
    collectionOfCommands.push_back(deviceName);
    collectionOfCommands.push_back(interruptProbability);
    collectionOfCommands.push_back(interruptTime);
}

When I run this I get no errors, but when I print the output of collectionOfCommands I get: 当我运行它时,我没有收到任何错误,但是当我打印collectionOfCommands的输出时,我得到了:

"Disk “磁盘

0.2 0.00 0.2 0.00

0.00005 0.00005

Disk 磁碟

0.2 0.00 0.2 0.00

Mouse 0.4 0.00002 鼠标0.4 0.00002

Disk 0.2 0.00 磁盘0.2 0.00

Keyboard 0.3 0.00004 键盘0.3 0.00004

Disk 0.2 0.00 磁盘0.2 0.00

Network 0.5 0.0001" 网络0.5 0.0001“

Obviously this output is completely wrong except for the very first output, "Disk." 显然,除了第一个输出“磁盘”之外,此输出是完全错误的。

Help would be much appreciated, thank you!!!! 帮助将不胜感激,谢谢!!

This is a weird way of breaking up a string especially since you already know of a consistent format. 这是一种拆分字符串的怪异方法,特别是因为您已经知道一致的格式。 Is there a paticular reason you are using substr()? 您使用substr()是否有特殊原因? Try using an input string stream instead. 尝试改用输入字符串流。

    #include <sstream>
    #include <string>
    ...

    istringstream iss(currentLine);

    getline(iss, deviceName, ' ');
    getline(iss, interruptProbability, ' ');
    getline(iss, interruptTime);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM