C++ 如何创建一个字符串数组,该数组拆分一个字符,该字符的单词之间用一个“"&引用;?c++;
我发现唯一想帮助我的问题就是这个。 我是C++新手,我需要一个字符串数组来记录我在这个字符中的每个单词。p> 代码如下:C++ 如何创建一个字符串数组,该数组拆分一个字符,该字符的单词之间用一个“"&引用;?c++;,c++,mobile,marmalade,C++,Mobile,Marmalade,我发现唯一想帮助我的问题就是这个。 我是C++新手,我需要一个字符串数组来记录我在这个字符中的每个单词。p> 代码如下: s3eFile* file = s3eFileOpen("chatTest/restrict_words.txt","rb"); int len = s3eFileGetSize(file); char* temp = new char[len]; if (file!=NULL) {
s3eFile* file = s3eFileOpen("chatTest/restrict_words.txt","rb");
int len = s3eFileGetSize(file);
char* temp = new char[len];
if (file!=NULL)
{
s3eFileRead(temp,len,1, file);
s3eFileClose(file);
}
所以我需要把这个temp转换成一个数组,这样我就可以使用它了?
有一种方法? 如果这是一个C++代码,那么我建议移到STD::string代替char *,并使用强大的STD工具,如FString、String String等。你指定的链接给出了如何做的详细答案
#include <string>
#include <sstream>
using namespace std;
.
.
.
s3eFile* file = s3eFileOpen("chatTest/restrict_words.txt","rb");
int len = s3eFileGetSize(file);
char* temp = new char[len];
if (file!=NULL)
{
s3eFileRead(temp,len,1, file);
//Adding Code here
string str(temp);
stringstream sstr(str)
vector<string> str_array;
string extracted;
while(sstr.good()){
sstr>>extracted;
str_array.push_back(extracted);
}
//at this point all the strings are in the array str_array
s3eFileClose(file);
}
#包括
#包括
使用名称空间std;
.
.
.
s3eFile*file=s3eFileOpen(“chatterst/restrict_words.txt”、“rb”);
int len=s3eFileGetSize(文件);
char*temp=新字符[len];
如果(文件!=NULL)
{
s3eFileRead(临时、len、1、文件);
//在这里添加代码
字符串str(temp);
stringstream sstr(str)
矢量stru阵列;
提取字符串;
while(sstr.good()){
sstr>>提取;
str_数组。推回(提取);
}
//此时,所有字符串都在数组str_数组中
s3eFileClose(文件);
}
<>您可以使用迭代器或简单的索引访问数组,如代码< > StryRoal[i],/Cudio> < p>如果这是C++代码,那么我建议移到STD::string代替char *,并使用强大的STD工具,如FString、String String等。你指定的链接给出了如何做的详细答案
#include <string>
#include <sstream>
using namespace std;
.
.
.
s3eFile* file = s3eFileOpen("chatTest/restrict_words.txt","rb");
int len = s3eFileGetSize(file);
char* temp = new char[len];
if (file!=NULL)
{
s3eFileRead(temp,len,1, file);
//Adding Code here
string str(temp);
stringstream sstr(str)
vector<string> str_array;
string extracted;
while(sstr.good()){
sstr>>extracted;
str_array.push_back(extracted);
}
//at this point all the strings are in the array str_array
s3eFileClose(file);
}
#包括
#包括
使用名称空间std;
.
.
.
s3eFile*file=s3eFileOpen(“chatterst/restrict_words.txt”、“rb”);
int len=s3eFileGetSize(文件);
char*temp=新字符[len];
如果(文件!=NULL)
{
s3eFileRead(临时、len、1、文件);
//在这里添加代码
字符串str(temp);
stringstream sstr(str)
矢量stru阵列;
提取字符串;
while(sstr.good()){
sstr>>提取;
str_数组。推回(提取);
}
//此时,所有字符串都在数组str_数组中
s3eFileClose(文件);
}
您可以使用迭代器或通过简单的索引来访问字符串,如数组str\u array[i]
可能类似于:
ifstream f("chatTest/restrict_words.txt");
vector<string> vec;
while (!f.fail())
{
string word;
f >> word;
vec.push_back(move(word));
}
ifstream f(“chatter/restrict_words.txt”);
向量向量机;
而(!f.fail())
{
字符串字;
f>>单词;
向量推回(移动(单词));
}
可能类似于:
ifstream f("chatTest/restrict_words.txt");
vector<string> vec;
while (!f.fail())
{
string word;
f >> word;
vec.push_back(move(word));
}
ifstream f(“chatter/restrict_words.txt”);
向量向量机;
而(!f.fail())
{
字符串字;
f>>单词;
向量推回(移动(单词));
}
既然您使用的是new,为什么不使用矢量而不是普通数组?矢量?我想没关系,我只需要知道如何使用iTunesstd::string::find
或它的一个亲戚。另请参见std::string::substr
。注意:char*temp
在使用它时相当于char temp[]
。如果您只想获取索引3,它是charc=temp[3]代码>。在使用C
构造时,只需注意空终止符。既然您使用的是new,为什么不使用向量而不是普通数组?向量?我想没关系,我只需要知道如何使用iTunesstd::string::find
或它的一个亲戚。另请参见std::string::substr
。注意:char*temp
在使用它时相当于char temp[]
。如果您只想获取索引3,它是charc=temp[3]代码>。在使用C
构造时,只需观察空终止符。在使用char*temp后,我如何清理它?我可以对s3eFile*文件执行相同的操作,对吗?我不知道s3eFileOpen和s3eFileClose函数。您必须查看它的文档/实现,才能确定是否释放/删除内存。@Ravi如果open分配内存,我希望close删除内存。但是你绝对不知道第三方分支。我使用后如何清理char*temp?我可以用s3eFile*文件做同样的事情,对吗?我不知道s3eFileOpen和s3eFileClose函数。您必须查看它的文档/实现,才能确定是否释放/删除内存。@Ravi如果open分配内存,我希望close删除内存。但是你绝对不会知道第三方分支的情况。哇move()
。这真的足够重要,可以在这个级别上引入吗?我希望它在.push_back()
…Wowmove()
中已经是自动的了。这真的足够重要,可以在这个级别上引入吗?我希望它对.push_back()
来说已经是自动的了。。。