如何在 C++ 中使用 substr 从右侧获取字符串

how to use substr get string from right hand side in c++

本文关键字:获取 字符串 substr C++      更新时间:2023-10-16

例如469111252我有一串数字,我知道如何从左侧拆分,但如何使用 substr 从右侧拆分它?

如果数字每个节点 = 2

左起 46 91 11 25 2

但我想从右边得到 52 12 11 69 4

//left hand side parse
for (int i=0;i<num.length(); i+=digitsPerNode) {
    splitNum = num.substr(i,digitsPerNode);
}

从上到下计数,而不是从下到上计数。

int numlength=num.length();
if(numlength%2==1)
numlength+=1;
    for(int i=numlength;i>=0;i-=digitsPerNode)
    {
        splitNum = num.substr(i,digitsPerNode);
    {
string a = "1234567890-=4";
for (int i = a.length() - 2; i >= -1; i = i - 2)
{
    if (i < 0) cout << a.substr(i+1, 1) << endl;
    else cout << a.substr(i, 2) << endl;
}

我找到了具有反向函数的解决方案

DoublyLinkedList::DoublyLinkedList(string value, string dPerNode)
{
    digitsPerNode = stoi(dPerNode);
    string subString;
    reverse(value.begin(), value.end());
    for (int i = 0; i <= value.length(); i += digitsPerNode)
    {
        //cut string by digitsPerNode Ex: 3 500 123 412
        subString = value.substr(i, digitsPerNode);
        reverse(subString.begin(), subString.end());
        insertAtHead(subString);
    }
}