Мне нужна помощь с этой программой. Я нахожусь в своем первом классе программирования и столкнулся с проблемой, пытаясь заставить мою программу работать. Я включил то, что я написал до сих пор, но все же он не компилируется. Указывает Ошибка:argument list for class template "std::vector" is missing
. : Комментарий:отсутствует список аргументов для шаблона класса «std :: vector»
Здесь возникает вопрос: Когда вы читаете длинный документ, есть много шансов, что многие слова встречаются несколько раз. Вместо хранения каждого слова может быть полезно хранить только уникальные слова и представлять документ как вектор указателей на уникальные слова. Напишите программу, которая реализует эту стратегию. Прочитайте слово за раз от 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);
}
}
}
Возможно, вы имели в виду 'vector words;'? –
Вы сделали 'include' –
therainmaker
Вы понимаете, что такое шаблон? Какую книгу вы используете? –