C++ C++;禁止从const char设置char*和char[]*
这是密码。我不知道为什么它不认识到它需要复制内存,我也不能强迫它C++ C++;禁止从const char设置char*和char[]*,c++,char,constants,C++,Char,Constants,这是密码。我不知道为什么它不认识到它需要复制内存,我也不能强迫它 string message="The quick brown fox jumped over the lazy dog."; vector<char*> words; while(message.length()>0){ char wtf[message.substr(0,message.find(" ")).c_str().length()]=message.substr(0,messa
string message="The quick brown fox jumped over the lazy dog.";
vector<char*> words;
while(message.length()>0){
char wtf[message.substr(0,message.find(" ")).c_str().length()]=message.substr(0,message.find(" ")).c_str();
words.push_back(wtf);
message=message.substr(message.find(" ")+1);
}
string message=“敏捷的棕色狐狸跳过了懒狗。”;
向量词;
while(message.length()>0){
char wtf[message.substr(0,message.find(“”).c_str().length()]=message.substr(0,message.find(“”).c_str();
字。推回(wtf);
message=message.substr(message.find(“”+1);
}
我看到有类似的线程,但在这方面没有。而且,C++似乎不能很容易地处理这个问题。 < P>代码>字符串< /COD> <代码>子字符串< /C>方法返回一个新的临时字符串,而<代码> CyStr< <代码>方法将指针返回到该临时字符串的内存中。 简单地说,将指针放入临时缓冲区会导致未定义的行为。
如果希望保留子字符串,请改用
string
类(即vector
)string
的substr
方法返回一个新的临时字符串,而c\u str
方法将指针直接返回到该临时字符串的内存中。
简单地说,将指针放入临时缓冲区会导致未定义的行为。
如果希望保留子字符串,请改用
string
类(即vector
)您想要数组中单词的副本,还是只需要指向原始单词的指针
如果需要副本,则需要字符串向量
此代码将创建单词的副本:
vector<string> words;
string::iterator it1 = message.begin();
string::iterator it2 = find(message.begin(), message.end(), ' ');
while (it2 != message.end())
{
words.push_back(string(it1, it2));
it1 = ++it2;
it2 = find(it2, message.end(), ' ');
}
words.push_back(string(it1, it2));
向量词;
字符串::迭代器it1=message.begin();
字符串::迭代器it2=find(message.begin(),message.end(),“”);
while(it2!=message.end())
{
字。推回(字符串(it1,it2));
it1=++it2;
it2=find(it2,message.end(),“”);
}
字。推回(字符串(it1,it2));
此代码将为您提供指向原始单词的指针:
vector<char*> words;
string::iterator it1 = message.begin();
string::iterator it2 = find(message.begin(), message.end(), ' ');
while (it2 != message.end())
{
words.push_back(&*it1);
it1 = ++it2;
it2 = find(it2, message.end(), ' ');
}
words.push_back(&*it1);
向量词;
字符串::迭代器it1=message.begin();
字符串::迭代器it2=find(message.begin(),message.end(),“”);
while(it2!=message.end())
{
字。推回(&*it1);
it1=++it2;
it2=find(it2,message.end(),“”);
}
字。推回(&*it1);
您想要数组中单词的副本,还是仅仅指向原始单词的指针
如果需要副本,则需要字符串向量
此代码将创建单词的副本:
vector<string> words;
string::iterator it1 = message.begin();
string::iterator it2 = find(message.begin(), message.end(), ' ');
while (it2 != message.end())
{
words.push_back(string(it1, it2));
it1 = ++it2;
it2 = find(it2, message.end(), ' ');
}
words.push_back(string(it1, it2));
向量词;
字符串::迭代器it1=message.begin();
字符串::迭代器it2=find(message.begin(),message.end(),“”);
while(it2!=message.end())
{
字。推回(字符串(it1,it2));
it1=++it2;
it2=find(it2,message.end(),“”);
}
字。推回(字符串(it1,it2));
此代码将为您提供指向原始单词的指针:
vector<char*> words;
string::iterator it1 = message.begin();
string::iterator it2 = find(message.begin(), message.end(), ' ');
while (it2 != message.end())
{
words.push_back(&*it1);
it1 = ++it2;
it2 = find(it2, message.end(), ' ');
}
words.push_back(&*it1);
向量词;
字符串::迭代器it1=message.begin();
字符串::迭代器it2=find(message.begin(),message.end(),“”);
while(it2!=message.end())
{
字。推回(&*it1);
it1=++it2;
it2=find(it2,message.end(),“”);
}
字。推回(&*it1);
OMG
char*message=“敏捷的棕色狐狸跳过了懒狗。”;
向量词;
int p=0;
while(消息[p])
{
words.push_back(&message[p++]);
}
//如果必须使用字符串,请更改为
words.push_back(&message.c_str()[p++]);
这就是,假设您需要指向每个字符的指针(我不知道您为什么希望这样做,但这就是您编码的内容)
顺便说一句,你想做什么?天哪
char*message=“敏捷的棕色狐狸跳过了懒狗。”;
向量词;
int p=0;
while(消息[p])
{
words.push_back(&message[p++]);
}
//如果必须使用字符串,请更改为
words.push_back(&message.c_str()[p++]);
这就是,假设您需要指向每个字符的指针(我不知道您为什么希望这样做,但这就是您编码的内容)
顺便说一句,您想做什么?您想按空格将字符串拆分为令牌。你应该使用合适的工具,例如。您的代码在以下几个方面是错误的:
c_str
返回一个只要字符串对象有效的指针char*
。使该向量保持std::string
c_str
返回一个char*
,它没有length
成员函数,也没有任何成员函数这表明你在C++中缺少一些基本知识。你也许应该读一读。所以,不,这不是C++的缺点。
真的,只需使用Boost.Tokenizer。文档中有一个按空格分割的示例。您想按空格将字符串分割为令牌。你应该使用合适的工具,例如。您的代码在以下几个方面是错误的:
c_str
返回一个只要字符串对象有效的指针char*
。使该向量保持std::string
c_str
返回一个char*
,它没有length
成员函数,也没有任何成员函数这表明你在C++中缺少一些基本知识。你也许应该读一读。所以,不,这不是C++的缺点。
真的,只需使用Boost.Tokenizer。它有一个在文档中按空格分割的示例。通常,您只需使用
向量。作为C++的一个通用规则,看到原始指针是一个巨大的警告标志——要么使用智能指针并动态分配内存,要么使用值类型——如代码> STD::String < /C> >。p> 通常情况下,您只会告诉我们
std::string message="The quick brown fox jumped over the lazy dog.";
std::stringstream stream(message);
std::string word;
stream >> word; // Reads "The"
stream >> word; // Reads "quick"
stream >> word; // Reads "brown" etc...
std::stringstream stream("The quick brown fox jumped over the lazy dog.");
std::istream_iterator<std::string> i(stream);
std::string word;
word = *i; // de-reference the iterator to get the object. Reads "The"
++i;
word = *i; ++i; // Reads "quick"
word = *i; ++i; // Reads "brown" etc
// Works for any type that uses >> to read from the stream
std::stringstream intstream("99 40 32 64 20 10 9 8 102");
std::istream_iterator<int> i(stream); // Notice the type is int here
int number;
number = *i; // de-reference the iterator to get the object. Reads "99"
++i;
number = *i; ++i; // Reads "44"
number = *i; ++i; // Reads "32" etc
int src[] = { 1, 2, 3, 4, 5, 6 };
int dst[] = { 0, 0, 0, 0, 0, 0 };
std::copy(src, src+6, dst); // copies src into dst
// It assumes there is enough space in dst
int src[] = { 1, 2, 3, 4, 5, 6 };
std::vector<int> dst; // Currently has zero size
std::copy(src, src+6, std::back_inserter(dst)); // copies src into dst
// back_inserter expands dst to fit
// by using push_back
// Create a stream from the string
std::stringstream stream(message);
// Use std::copy to copy from the string.
// The stream is iterated over a word at a time.
// because the istream iterator is using std::string type.
//
// The istream_iterator with no parameters is equivelent to end.
//
// The destination appends the word to the vector words.
std::copy(std::istream_iterator<std::string>(stream), // 2: Copy words from the stream
std::istream_iterator<std::string>(),
std::back_inserter(words)); // into the back of the vector.