我需要一些帮助,这程序。我正在参加我的第一个编程课,并试图让我的程序开始工作。我已经包括了迄今为止所写的内容,但仍然没有编译。它给出了错误:argument list for class template "std::vector" is missing
。参数列表中缺少
这里是一个问题: 当你阅读一个长文档时,很多单词很可能会多次出现。不是存储每个单词,只存储唯一单词并将该文档表示为指向唯一单词的向量的向量可能是有益的。编写一个实现这个策略的程序。从cin
一次读一个字。保留一个vector <char *>
的文字。如果新的单词不存在于这个向量中,请分配内存,将单词复制到内存中,并将指针附加到新的内存中。如果这个单词已经存在,那么附加一个指向现有单词的指针。
下面的代码片段:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
/* Create a vector of char pointers to hold the individual words.
Create a string input to hold the next input through cin. */
int main() {
vector words;
string input;
/* Keep the while loop running using cin as the condition to read an entire document.
This will end when a document has reached its end. */
while (cin >> input) {
/* For every word read as a string, convert the word into a c-string by allocating
a new character array with the proper size and using c_str and strcpy to copy
an identical c-string into the memory heap. */
char* temp = new char[input.length() + 1];
strcpy(temp, input.c_str());
/* Next, check if the word is already in the words array. Use a boolean variable
that updates if the word is found. Compare words by using the strcmp function;
when they are equal, strcmp equals 0. */
bool already_present = false;
for (int i = 0; i < words.size(); i++) {
if (strcmp(temp, words[i]) == 0) {
already_present = true;
}
}
/* If the word is already present, delete the allocated memory.
Otherwise, push the pointer into the words vector. */
if (already_present) {
delete temp;
} else {
words.push_back(temp);
}
}
}
您的意思是写'矢量词;'? –
你做了'包括' –
therainmaker
你没有意识到的模板是什么?你在用什么书? –