将字符串分割为向量(C++)

12

我写了一段简单的代码,将字符串按照每个'/'分割并存储到向量中。 我的字符串可能以'/'开头也可能不是,但一定会以'/'结尾。例如,如果我的字符串是:

string="/home/desktop/test/" 
I want to <"/","home","desktop","test"> and another example

string="../folder1/folder2/../pic.pdf/" 
I want to store <"..","folder1","folder2","..","pic.pdf"

然而,我的代码为我提供了第一个示例的 <" ","home","desktop","test"," "> 和第二个示例的 <"..","folder1","folder2","..","pic.pdf"," ">

有人能帮帮我吗? 这是我用于此的代码:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string strLine("/cd/desktop/../test/");
    string strTempString;
    vector<int> splitIndices;
    vector<string> splitLine;
    int nCharIndex = 0;
    int nLineSize = strLine.size();

    // find indices
    for(int i = 0; i < nLineSize; i++)
    {
        if(strLine[i] == '/')
            splitIndices.push_back(i);
    }
    splitIndices.push_back(nLineSize); // end index

    // fill split lines
    for(int i = 0; i < (int)splitIndices.size(); i++)
    {
        strTempString = strLine.substr(nCharIndex, (splitIndices[i] - nCharIndex));
        splitLine.push_back(strTempString);
        cout << strTempString << endl;
        nCharIndex = splitIndices[i] + 1;
    }


}

1
是的,但他想要帮助他的代码,而不是从头开始。 - RichardPlunkett
1
看起来你正在寻找解析文件系统路径的方法。也许可以使用Boost.filesystem - user529758
一个简单的hack能完成任务吗?从i=1开始第一个循环,到末尾前一个字符停止? - RichardPlunkett
@RichardPlunkett 你怎么知道呢?如果他真正想要解决的问题是分解路径,但他认为没有现成的解决方案呢? - user529758
strtok可能会有用 - gelatine1
显示剩余2条评论
4个回答

8

1
图书馆在2017年被删除了。 - Edward Gaere

1

在代码中有一些需要做的事情来解决这个问题,可能有更好、更优雅的解决方案。

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    string strLine("/cd/desktop/../test/");
    string strTempString;
    vector<int> splitIndices;
    vector<string> splitLine;
    int nCharIndex = 0;
    int nLineSize = strLine.size();

    // find indices
    if(nLineSize!=0 && strLine[0]=='/')
    {
         splitLine.push_back(strLine.substr(0,1));
         nCharIndex++;
    }

    for(int i = 1; i < nLineSize; i++)
    {
        if(strLine[i] == '/')
            splitIndices.push_back(i);
    }

    // fill split lines
    for(int i = 0; i <int(splitIndices.size()); i++)
    {
        strTempString = strLine.substr(nCharIndex, (splitIndices[i] - nCharIndex));
        splitLine.push_back(strTempString);
        nCharIndex = splitIndices[i] + 1;
    }        
}

编辑:

清理了代码,现在可以删除添加最后一个索引部分。

编辑2:

可能更优雅的解决方案是通过移除ncharcounter并使用您的分割索引来实现。如果第一个字符不是('/'),则可以将第一个值存储为'-1',如果是,则存储为('0')。

    string strLine("/cd/desktop/../test/");
    string strTempString;
    vector<int> splitIndices;
    vector<string> splitLine;
    int nLineSize = strLine.size();

    // find indices
    splitIndices.push_back(-1);
    if(nLineSize!=0 && strLine[0]=='/')
    {
         splitLine.push_back(strLine.substr(0,1));
         splitIndices[0]=0;
    }             
    for(int i = 1; i < nLineSize; i++)
    {
        if(strLine[i] == '/')
            splitIndices.push_back(i);
    }

    // fill split lines
    for(int i = 1; i <int(splitIndices.size()); i++)
    {
        strTempString = strLine.substr(splitIndices[i-1]+1, (splitIndices[i] - (splitIndices[i-1]+1) ));
        splitLine.push_back(strTempString);
    }        

1
以下更改可能有所帮助:

if (!strLine.empty() && strLine.back() != '/') {
    splitIndices.push_back(nLineSize); // end index
}
// fill split lines
for(int i = 0; i < (int)splitIndices.size(); i++)
{
    if (splitIndices[i] == 0) {
        splitLine.push_back("/");
    } else {
        strTempString = strLine.substr(nCharIndex, (splitIndices[i] - nCharIndex));
        splitLine.push_back(strTempString);
    }
    nCharIndex = splitIndices[i] + 1;
}

0
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main()
{
    std::string s1 = "/home/desktop/test/";
    std::string s2 = "../folder1/folder2/../pic.pdf/";

    std::vector<std::string> v1;

    std::istringstream is1( s1 );

    std::string t;

    while ( std::getline( is1, t, '/' ) )
    {
        if ( t.empty() ) v1.push_back( "/" );
        else v1.push_back( t );
    }

    for ( const std::string &t : v1 ) std::cout << t << std::endl;

    std::cout << std::endl;

    std::vector<std::string> v2;

    std::istringstream is2( s2 );

    while ( std::getline( is2, t, '/' ) )
    {
        if ( t.empty() ) v2.push_back( "/" );
        else v2.push_back( t );
    }

    for ( const std::string &t : v2 ) std::cout << t << std::endl;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接