Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用子字符串C+获得奇怪的输出+;_C++_Visual Studio 2010_Vector_Substring - Fatal编程技术网

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

C++ 使用子字符串C+获得奇怪的输出+;,c++,visual-studio-2010,vector,substring,C++,Visual Studio 2010,Vector,Substring,所以我有一些奇怪的问题,看起来很简单 我有一个向量: 向量(字符串)集合燧石 它保存我从.txt文件中获得的文本行。文本文件的内容包括: "四, 磁盘0.2 0.00005 鼠标0.40.00002 键盘0.3 0.00004 网络0.5 0.0001“ CollectionOfflines[0]=“磁盘0.2 0.00005” 我试图将此字符串分为三个不同的字符串:“磁盘”、“0.2”和“0.00005”,然后将这些字符串放入不同的向量中: 向量(字符串)集合命令 这是我从行字符串中获取子字符

所以我有一些奇怪的问题,看起来很简单

我有一个向量:

向量(字符串)集合燧石

它保存我从.txt文件中获得的文本行。文本文件的内容包括:

"四,

磁盘0.2 0.00005

鼠标0.40.00002

键盘0.3 0.00004

网络0.5 0.0001“

CollectionOfflines[0]=“磁盘0.2 0.00005”

我试图将此字符串分为三个不同的字符串:“磁盘”、“0.2”和“0.00005”,然后将这些字符串放入不同的向量中:

向量(字符串)集合命令

这是我从行字符串中获取子字符串并将其放入新向量的循环

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);
}
字符串设备名称;
串中断概率;
字符串中断时间;
对于(inti=1;i
当我运行此命令时,我没有收到任何错误,但当我打印CollectionofCommand的输出时,我得到:

“磁盘

0.20.00

0.00005

圆盘

0.20.00

鼠标0.40.00002

磁盘0.20.00

键盘0.3 0.00004

磁盘0.20.00

网络0.5 0.0001“

很明显,这个输出是完全错误的,除了第一个输出“Disk”


非常感谢您的帮助,谢谢

这是一种奇怪的拆分字符串的方法,尤其是因为您已经知道了一致的格式。使用substr()是否有特殊原因?尝试改用输入字符串流

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

    istringstream iss(currentLine);

    getline(iss, deviceName, ' ');
    getline(iss, interruptProbability, ' ');
    getline(iss, interruptTime);
#包括
#包括
...
istringstream iss(电流线);
getline(iss,deviceName,”);
getline(iss,中断概率,”);
getline(iss,中断时间);

调试器向您显示了什么?所以我才想出了自己的问题。来自java的substring方法具有不同的参数。与substr()相比,它们是更好的方法来完成我正在尝试的任务吗?我认为我不能再使用substr(),因为我的文本文件中的行的长度不会都与上一行相同。