C++ 我的结构是一个单词列表,之后必须输出。但是输出格式是错误的 P> >,我必须将char *转换成我自己的StReStsList.这只是前一个数组中的单词列表(用空格分隔),然后创建一个OFASSWATE = A'和& Word(index),如注释所示,
我的结构是一个单词列表,之后必须输出。但是输出格式是错误的C++ 我的结构是一个单词列表,之后必须输出。但是输出格式是错误的 P> >,我必须将char *转换成我自己的StReStsList.这只是前一个数组中的单词列表(用空格分隔),然后创建一个OFASSWATE = A'和& Word(index),如注释所示,,c++,C++,我的结构是一个单词列表,之后必须输出。但是输出格式是错误的 P> >,我必须将char *转换成我自己的StReStsList.这只是前一个数组中的单词列表(用空格分隔),然后创建一个OFASSWATE = A'和& Word(index),如注释所示,使用C++类型使问题更好地可读和理解. #include <iostream> using namespace std; using Word = std:: string; using SList = std::vector&l
<> P> >,我必须将char *转换成我自己的StReStsList.这只是前一个数组中的单词列表(用空格分隔),然后创建一个OFASSWATE = A'和& Word(index)
,如注释所示,使用C++类型使问题更好地可读和理解.
#include <iostream>
using namespace std;
using Word = std:: string;
using SList = std::vector<Word>;
SList third(std::string const &words) {
SList myArray;
int index = 0;
while (index < words.size()) {
Word word;
while (words[index] >= 'a' && words[index] <= 'z') {
word += words[index];
index++;
}
myArray.push_back( word);
cout << word << ' ' << myArray.back() << std::endl;
}
cout << endl;
return myArray;
}
// I believe this operator is actually undefined behavior as you ain't allowed to write this method for standard types.
ostream &operator<<(ostream &os, const SList &list)
{
for (auto &&word : list)
os << word << std::endl;
return os;
}
int main() {
std::string chars {"ezesc eft yyyyyy fh"};
SList list = third(chars);
cout << list << endl;
system("pause");
}
#包括 使用名称空间std; 使用Word=std::string; 使用SList=std::vector; 第三个列表(标准::字符串常量和单词){ 滑动阵列; int指数=0; while(索引
?如果这被教导为C++中的工作方式,难怪学生在完成课程后就放弃C++。这实际上是一个6行的程序,如果C++被有效地使用。d在处理完对象后(在指向该对象的最后一个指针被销毁之前)但是你还没有完成这个对象。虽然如果你用while(words[index]>='a'&&words[index]欢迎使用Stack Overflow!听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看它偏离预期的位置。如果您要进行任何编程,这是一个必不可少的工具。进一步阅读:您已经删除了所有单词yoUR数组试图指向。为什么不简单地使用<代码> STD::Vector 和<代码> STD::ISTIGINSTROUND 而不是std::string
,你就不会有这些问题了。@АааСааааааааааааааааааа如果老师说使用char*
STD::String ,同 char */COD>的学生有相同的问题: STD::String ,但在程序的不同方面,C++的目标之一,至少根据语言的发明者,是通过使用高级构造来减少错误。不是为了让事情变得更难。 #include <iostream> using namespace std; struct Word { int size; char *symbols; }; struct SList { int size; Word *myArray; }; SList third(char *words, int size, size_t n) { int indexOfMyArray = 0; int index = 0; Word *myArray = new Word[size]; for (int i = 0; i < size; i++) { char *word = new char[n]; int k = 0; while (words[index] >= 'a' && words[index] <= 'z') { word[k] = words[index]; k++; index++; } word[k] = '\0'; index++; Word word1 = {k, word}; myArray[indexOfMyArray] = word1; indexOfMyArray++; cout << word << ' '; for (int k = 0; k < word1.size; k++) cout << word1.symbols[k]; cout << endl; delete[](word); } cout << endl; SList list = {size, myArray}; return list; } ostream &operator<<(ostream &os, const SList &list) { for (int i = 0; i < list.size; i++) { for (int j = 0; j < list.myArray[i].size; j++) { os << static_cast<char>(list.myArray[i].symbols[j]+0); } os << endl; } return os; } int main() { char chars[19] = {'e', 'z', 'e', 's', 'c', ' ', 'e', 'f', 't', ' ', 'y', 'y', 'y', 'y', 'y', 'y', ' ', 'f', 'h'}; SList list = third(chars, 4, 19); cout << list << endl; system("pause"); }
#include <iostream> using namespace std; using Word = std:: string; using SList = std::vector<Word>; SList third(std::string const &words) { SList myArray; int index = 0; while (index < words.size()) { Word word; while (words[index] >= 'a' && words[index] <= 'z') { word += words[index]; index++; } myArray.push_back( word); cout << word << ' ' << myArray.back() << std::endl; } cout << endl; return myArray; } // I believe this operator is actually undefined behavior as you ain't allowed to write this method for standard types. ostream &operator<<(ostream &os, const SList &list) { for (auto &&word : list) os << word << std::endl; return os; } int main() { std::string chars {"ezesc eft yyyyyy fh"}; SList list = third(chars); cout << list << endl; system("pause"); }