I am trying to insert a string separated by spaces into an array of strings without using vector in C++. For example:
在c++中,我试图将一个由空格分隔的字符串插入到一个字符串数组中,而不使用vector。例如:
using namespace std;
int main() {
string line = "test one two three.";
string arr[4];
//codes here to put each word in string line into string array arr
for(int i = 0; i < 4; i++) {
cout << arr[i] << endl;
}
}
I want the output to be:
我希望输出是:
test
one
two
three.
I know there are already a lot of questions asking string > array in C++. I realize this might be a duplicate question, but I could not find any answer satisfying my conditions (splitting a string into an array WITHOUT using vector). I apologize in advanced if this was a repeated question.
我知道在c++中已经有很多问题在问字符串>数组。我意识到这可能是一个重复的问题,但是我找不到任何满足我的条件的答案(不使用向量就将一个字符串分割成一个数组)。如果这是一个重复的问题,我提前道歉。
5 个解决方案
#1
32
It is possible to turn the string into a stream by using the std::stringstream
class (its constructor takes a string as parameter). Once it's built, you can use the >>
operator on it (like on regular file based streams), which will extract, or tokenize word from it:
可以使用std::stringstream类(其构造函数以字符串作为参数)将字符串转换为流。构建完成后,您可以在其上使用>>操作符(类似于基于常规文件的流),它将从中提取或标记单词:
#include <sstream>
using namespace std;
int main(){
string line = "test one two three.";
string arr[4];
int i = 0;
stringstream ssin(line);
while (ssin.good() && i < 4){
ssin >> arr[i];
++i;
}
for(i = 0; i < 4; i++){
cout << arr[i] << endl;
}
}
#2
2
#include <iostream>
#include <sstream>
#include <iterator>
#include <string>
using namespace std;
template <size_t N>
void splitString(string (&arr)[N], string str)
{
int n = 0;
istringstream iss(str);
for (auto it = istream_iterator<string>(iss); it != istream_iterator<string>() && n < N; ++it, ++n)
arr[n] = *it;
}
int main()
{
string line = "test one two three.";
string arr[4];
splitString(arr, line);
for (int i = 0; i < 4; i++)
cout << arr[i] << endl;
}
#3
2
#define MAXSPACE 25
string line = "test one two three.";
string arr[MAXSPACE];
string search = " ";
int spacePos;
int currPos = 0;
int k = 0;
int prevPos = 0;
do
{
spacePos = line.find(search,currPos);
if(spacePos >= 0)
{
currPos = spacePos;
arr[k] = line.substr(prevPos, currPos - prevPos);
currPos++;
prevPos = currPos;
k++;
}
}while( spacePos >= 0);
arr[k] = line.substr(prevPos,line.length());
for(int i = 0; i < k; i++)
{
cout << arr[i] << endl;
}
#4
1
Trivial:
微不足道的:
const vector<string> explode(const string& s, const char& c)
{
string buff{""};
vector<string> v;
for(auto n:s)
{
if(n != c) buff+=n; else
if(n == c && buff != "") { v.push_back(buff); buff = ""; }
}
if(buff != "") v.push_back(buff);
return v;
}
按照链接
#5
0
Here's a suggestion: use two indices into the string, say start
and end
. start
points to the first character of the next string to extract, end
points to the character after the last one belonging to the next string to extract. start
starts at zero, end
gets the position of the first char after start
. Then you take the string between [start..end)
and add that to your array. You keep going until you hit the end of the string.
这里有一个建议:在字符串中使用两个指标,比如开始和结束。开始点到下一个字符串的第一个字符提取,结束点到字符后面的字符后,属于下一个字符串提取。start从0开始,end获取start之后第一个char的位置。然后取[start. end]之间的字符串,并将其添加到数组中。你一直走,直到你到达弦的末端。
#1
32
It is possible to turn the string into a stream by using the std::stringstream
class (its constructor takes a string as parameter). Once it's built, you can use the >>
operator on it (like on regular file based streams), which will extract, or tokenize word from it:
可以使用std::stringstream类(其构造函数以字符串作为参数)将字符串转换为流。构建完成后,您可以在其上使用>>操作符(类似于基于常规文件的流),它将从中提取或标记单词:
#include <sstream>
using namespace std;
int main(){
string line = "test one two three.";
string arr[4];
int i = 0;
stringstream ssin(line);
while (ssin.good() && i < 4){
ssin >> arr[i];
++i;
}
for(i = 0; i < 4; i++){
cout << arr[i] << endl;
}
}
#2
2
#include <iostream>
#include <sstream>
#include <iterator>
#include <string>
using namespace std;
template <size_t N>
void splitString(string (&arr)[N], string str)
{
int n = 0;
istringstream iss(str);
for (auto it = istream_iterator<string>(iss); it != istream_iterator<string>() && n < N; ++it, ++n)
arr[n] = *it;
}
int main()
{
string line = "test one two three.";
string arr[4];
splitString(arr, line);
for (int i = 0; i < 4; i++)
cout << arr[i] << endl;
}
#3
2
#define MAXSPACE 25
string line = "test one two three.";
string arr[MAXSPACE];
string search = " ";
int spacePos;
int currPos = 0;
int k = 0;
int prevPos = 0;
do
{
spacePos = line.find(search,currPos);
if(spacePos >= 0)
{
currPos = spacePos;
arr[k] = line.substr(prevPos, currPos - prevPos);
currPos++;
prevPos = currPos;
k++;
}
}while( spacePos >= 0);
arr[k] = line.substr(prevPos,line.length());
for(int i = 0; i < k; i++)
{
cout << arr[i] << endl;
}
#4
1
Trivial:
微不足道的:
const vector<string> explode(const string& s, const char& c)
{
string buff{""};
vector<string> v;
for(auto n:s)
{
if(n != c) buff+=n; else
if(n == c && buff != "") { v.push_back(buff); buff = ""; }
}
if(buff != "") v.push_back(buff);
return v;
}
按照链接
#5
0
Here's a suggestion: use two indices into the string, say start
and end
. start
points to the first character of the next string to extract, end
points to the character after the last one belonging to the next string to extract. start
starts at zero, end
gets the position of the first char after start
. Then you take the string between [start..end)
and add that to your array. You keep going until you hit the end of the string.
这里有一个建议:在字符串中使用两个指标,比如开始和结束。开始点到下一个字符串的第一个字符提取,结束点到字符后面的字符后,属于下一个字符串提取。start从0开始,end获取start之后第一个char的位置。然后取[start. end]之间的字符串,并将其添加到数组中。你一直走,直到你到达弦的末端。