[英]splitting string into vector c++
我寫了一個簡單的代碼來分割每個'/'的字符串並存儲到vector中。 我的字符串可以以/或不開頭,並且definetelly將以/結尾。 例如,如果我的字符串是:
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;
}
}
C ++字符串工具包庫(Strtk)具有以下解決方案:
http://www.codeproject.com/Articles/23198/C-String-Toolkit-StrTk-Tokenizer
在應該解決這個問題的代碼中要做的一些事情,可能會有一個更好,更優雅的解決方案。
#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);
}
更改后可能有所幫助:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.