迭代字符串片段

时间:2017-08-16 12:03:02

标签: c++

我有lua路径,例如:

"/home/user/?.lua;/home/test/?/init.lua;./lua"

我想迭代每个片段(示例中为/home/user/?.lua/home/test/?/init.lua./lua)。由于某些原因,我的尝试无法正常工作:

size_t begin = 0;
size_t next = searchpath.find_first_of(";", 0);
do
{
    if (next == std::string::npos)
        next = searchpath.length();
    std::string prefix = searchpath.substr(begin, next);
    std::cout << "Trying: " << prefix << "\n";
    begin = next + 1;
    next = searchpath.find_first_of(";", begin);
} while (begin < (int)searchpath.length());

输出:

Trying: /home/v/.luarocks/share/lua/5.2/?.lua
Trying: /home/v/.luarocks/share/lua/5.2/?/init.lua;/usr/share/lua/5.2/?.lua;/usr/share/l
Trying: /usr/share/lua/5.2/?.lua;/usr/share/lua/5.2/?/init.lua;/usr/local/share/lua/5.2/?.lua;/usr/local/share/lu
Trying: /usr/share/lua/5.2/?/init.lua;/usr/local/share/lua/5.2/?.lua;/usr/local/share/lua/5.2/?/init.lua;/usr/local/lib/lua/5.2/?.lua;/usr/loca
Trying: /usr/local/share/lua/5.2/?.lua;/usr/local/share/lua/5.2/?/init.lua;/usr/local/lib/lua/5.2/?.lua;/usr/local/lib/lua/5.2/?/init.lua;./?.lua;/home/v/.lua/libs/?.lua;/hom
Trying: /usr/local/share/lua/5.2/?/init.lua;/usr/local/lib/lua/5.2/?.lua;/usr/local/lib/lua/5.2/?/init.lua;./?.lua;/home/v/.lua/libs/?.lua;/home/v/.lua/libs/?.lua;./lib/?.lua;./lib/?/init.lua
Trying: /usr/local/lib/lua/5.2/?.lua;/usr/local/lib/lua/5.2/?/init.lua;./?.lua;/home/v/.lua/libs/?.lua;/home/v/.lua/libs/?.lua;./lib/?.lua;./lib/?/init.lua
Trying: /usr/local/lib/lua/5.2/?/init.lua;./?.lua;/home/v/.lua/libs/?.lua;/home/v/.lua/libs/?.lua;./lib/?.lua;./lib/?/init.lua
Trying: ./?.lua;/home/v/.lua/libs/?.lua;/home/v/.lua/libs/?.lua;./lib/?.lua;./lib/?/init.lua
Trying: /home/v/.lua/libs/?.lua;/home/v/.lua/libs/?.lua;./lib/?.lua;./lib/?/init.lua
Trying: /home/v/.lua/libs/?.lua;./lib/?.lua;./lib/?/init.lua
Trying: ./lib/?.lua;./lib/?/init.lua

请问您能解释一下我的代码有什么问题以及正确的方法是什么?

3 个答案:

答案 0 :(得分:2)

问题是:

    std::string prefix = searchpath.substr(begin, next);

std::string::substr接受offset + count参数,而不是start + end参数。

你需要:

    std::string prefix = searchpath.substr(begin, next-begin);

完全如此:

#include <iostream>
#include <string>

int main()
{
    std::string searchpath = "a;b;c";
    size_t begin = 0;
    size_t next = searchpath.find_first_of(";", 0);
    do
    {
        if (next == std::string::npos)
            next = searchpath.length();
        std::string prefix = searchpath.substr(begin, next-begin);
        std::cout << "Trying: " << prefix << "\n";
        begin = next + 1;
        next = searchpath.find_first_of(";", begin);
    } while (begin < (int)searchpath.length());
}

(注意,这就是你的问题应该是什么样的。一个完整的程序。)

我实际上会写为:

#include <iostream>
#include <string>

int main()
{
    std::string searchpath = "a;b;c";
    for (size_t begin = 0, next; begin < searchpath.length(); begin = next+1)
    {
        next = searchpath.find(';', begin);
        if (next == std::string::npos)
            next = searchpath.length();
        std::string prefix = searchpath.substr(begin, next-begin);
        std::cout << "Trying: " << prefix << "\n";
    }
}

重复次数较少,本地化开始和循环旁边,使用find而非find_first_of,并且没有不必要的演员。

答案 1 :(得分:1)

除了@Martin Bonner的答案之外。

在我看来,它可以更简单,更易读,检查出来。

char someInput[] = "/home/user/?.lua;/home/test/?/init.lua;./lua";

std::string searchpath(someInput);
std::string temp;
std::size_t pos = std::string::npos;

pos = searchpath.find(";");
while (pos != std::string::npos)
{
    temp = searchpath.substr(0, pos);
    searchpath = searchpath.substr(pos + 1);
    pos = searchpath.find(";");

    std::cout << "Trying: " << temp << std::endl;
}
std::cout << searchpath << std::endl;

输出

Trying: /home/user/?.lua
Trying: /home/test/?/init.lua
Trying: ./lua

答案 2 :(得分:0)

在这里,使用std::getline()的简单版本:

Work Ref XXXX
Country of Origin XXX
Sponsor/Publisher/Owner XXX
Significant Contributors XXXX

XXX Library
Product Type XXX
Country XX
Identifier XXXX-XXXX
Release/Go-Live Date 01/08/2010

XXXX
Sub Title
Series
Format XXXX
Extent
Sales Rights World All Languages

Country XXX
XX Pub/Release Date 01/08/2010
XX Confirmation Status Confirmed
XX Classification XX
XX Sub Division XXX
XX Price Inc Tax 0.0
XX Tax Rate 0
XX Taxable % 100.00
XX Availability Status Available

demo

<强>输出

  

尝试:/home/user/?.lua
  尝试:/home/test/?/init.lua
  尝试:./ lua