需要将数字与行上的字符串分开,以“;”分隔,(25;16;67;13)在C++中

需要将数字与行上的字符串分开,以“;”分隔,(25;16;67;13)在C++中,c++,string,loops,while-loop,find,C++,String,Loops,While Loop,Find,我们有一个字符串25;16;67;13;14;....... 我们需要把数字分开打印出来。最后一个数字后面没有分号 输出应该是这样的: 25 16 67 13 14 ...... 假设我们使用str.find、str.substr和size\t变量current\u pos、prev\u pos,那么我们用来浏览行的while循环的条件是什么,以便它打印出所有数字,而不仅仅是第一个数字?您可以使用std::istringstream: 您可以使用std::istringstream: 如果只需

我们有一个字符串25;16;67;13;14;.......

我们需要把数字分开打印出来。最后一个数字后面没有分号

输出应该是这样的:

25
16
67
13
14
......
假设我们使用str.find、str.substr和size\t变量current\u pos、prev\u pos,那么我们用来浏览行的while循环的条件是什么,以便它打印出所有数字,而不仅仅是第一个数字?

您可以使用std::istringstream:

您可以使用std::istringstream:


如果只需要在字符串中打印数字,而不需要在数据结构中表示数字,那么解决方案非常简单。只需读取整个字符串,然后逐字符打印即可。如果字符是分号,则打印新行

#include <iostream>
#include <string>

using namespace std;

int main(){
    string input;
    cin >> input;
    for(int i = 0; i < input.length(); i++){
        if(input.at(i) == ';') cout << endl;
        else cout << input.at(i);
    }
}

如果只需要在字符串中打印数字,而不需要在数据结构中表示数字,那么解决方案非常简单。只需读取整个字符串,然后逐字符打印即可。如果字符是分号,则打印新行

#include <iostream>
#include <string>

using namespace std;

int main(){
    string input;
    cin >> input;
    for(int i = 0; i < input.length(); i++){
        if(input.at(i) == ';') cout << endl;
        else cout << input.at(i);
    }
}

我将用一个例子和一条单行线的替代解决方案为您的问题提供准确答案

请看

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <regex>

const std::regex re(";");

int main() {

    std::string test("25;16;67;13;14;15");

    // Solution 1: as requested
    {
        size_t current_pos{};
        size_t prev_pos{};
        // Search for the next semicolon
        while ((current_pos = test.find(';', prev_pos)) != std::string::npos) {

            // Print the resulting value
            std::cout << test.substr(prev_pos, current_pos - prev_pos) << "\n";
            // Update search positions
            prev_pos = current_pos + 1;
        }
        // Since there is no ; at the end, we print the last number manually
        std::cout << test.substr(prev_pos) << "\n\n";
    }

    // Solution 2. All in one statement. Just to show to you what can be done with C++
    {
        std::copy(std::sregex_token_iterator(test.begin(), test.end(), re, -1), {}, std::ostream_iterator<std::string>(std::cout, "\n"));
    }
    return 0;
}

我将用一个例子和一条单行线的替代解决方案为您的问题提供准确答案

请看

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <regex>

const std::regex re(";");

int main() {

    std::string test("25;16;67;13;14;15");

    // Solution 1: as requested
    {
        size_t current_pos{};
        size_t prev_pos{};
        // Search for the next semicolon
        while ((current_pos = test.find(';', prev_pos)) != std::string::npos) {

            // Print the resulting value
            std::cout << test.substr(prev_pos, current_pos - prev_pos) << "\n";
            // Update search positions
            prev_pos = current_pos + 1;
        }
        // Since there is no ; at the end, we print the last number manually
        std::cout << test.substr(prev_pos) << "\n\n";
    }

    // Solution 2. All in one statement. Just to show to you what can be done with C++
    {
        std::copy(std::sregex_token_iterator(test.begin(), test.end(), re, -1), {}, std::ostream_iterator<std::string>(std::cout, "\n"));
    }
    return 0;
}
} //任何标点符号,/;:=

}
//任何标点符号,/;:=

这回答了你的问题吗?这回答了你的问题吗?需要的基本库:包含需要的基本库:包含
using namespace std;
int main() {
string a{ "1232,12312;21414:231;23231;22" };
for (int i = 0; i < a.size(); i++) {
    if (ispunct(a[i])) {
        a[i] = ' ';
    }
}
stringstream line(a);
string b;
while (getline(line, b, ' ')) {
    cout << b << endl;

}