C++ 将字符串拆分为向量c++;

C++ 将字符串拆分为向量c++;,c++,string,vector,split,C++,String,Vector,Split,我编写了一个简单的代码,将字符串从每个“/”中拆分并存储到向量中。 我的字符串可以以/开头,也可以不以/结尾。例如,如果我的字符串是: string="/home/desktop/test/" I want to <"/","home","desktop","test"> and another example string="../folder1/folder2/../pic.pdf/" I want to store <"..","folder1","folder2"

我编写了一个简单的代码,将字符串从每个“/”中拆分并存储到向量中。 我的字符串可以以/开头,也可以不以/结尾。例如,如果我的字符串是:

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"
string=“/home/desktop/test/”
我想再举一个例子
string=“../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
    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;
    }        
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串strLine(“/cd/desktop/。/test/”);
字符串strTempString;
向量分裂指数;
矢量分裂线;
int-nCharIndex=0;
int nLineSize=strLine.size();
//查找索引
if(nLineSize!=0&&strLine[0]='/'))
{
splitLine.push_-back(strLine.substr(0,1));
nCharIndex++;
}
对于(int i=1;i对于(int i=0;i以下更改可能会有所帮助:

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;
}
if(!strLine.empty()&&strLine.back()!='/')){
splitindex.push_back(nLineSize);//结束索引
}
//填充分割线
对于(int i=0;i<(int)splitIndexes.size();i++)
{
如果(拆分索引[i]==0){
拆分线。将_向后推(“/”;
}否则{
strTempString=strLine.substr(nCharIndex,(SplitIndex[i]-nCharIndex));
拆分线。推回(strTempString);
}
nCharIndex=分裂指数[i]+1;
}
#包括
#包括
#包括
#包括
int main()
{
std::string s1=“/home/desktop/test/”;
std::string s2=“../folder1/folder2/。/pic.pdf/”;
std::向量v1;
std::istringstream为1(s1);
std::字符串t;
while(std::getline(is1,t,“/”)
{
如果(t.empty())v1.向后推(“/”);
else v1.推回(t);
}

对于(const STD::Strand &t:V1)STD::CUT

C++字符串工具包库(STRTK)对您的问题有以下解决方案:


可能重复是的,但他需要代码方面的帮助,rahter than from Scratch似乎您正在寻找解析文件系统路径。可能使用?一个简单的黑客可以完成这项工作吗?从i=1开始第一个循环,在结束前停止一个字符?@RichardPlunkett你怎么知道?如果他试图解决的真正问题是分解但是他认为还没有一个现成的解决方案?
    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);
    }        
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;
}
#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;
}