2015-11-15 137 views
-5

我需要一些帮助,这程序。我正在参加我的第一个编程课,并试图让我的程序开始工作。我已经包括了迄今为止所写的内容,但仍然没有编译。它给出了错误: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); 
     } 
    } 
} 
+1

您的意思是写'矢量词;'? –

+1

你做了'包括' – therainmaker

+1

你没有意识到的模板是什么?你在用什么书? –

回答

0

我希望下面的代码片段可能会有所帮助:

#include <string> 
#include <iostream> 
#include <string.h>  // String.h for strcmp() 
#include <vector>   // Vector Header file is added 
using namespace std; 

int main() { 
    vector <char *> words;  // vector of char * 
    string input; 

    while (cin >> input) { 
     char *temp = new char[input.length() + 1]; 
     strcpy(temp, input.c_str()); 

     bool already_present = false; 

     for (unsigned int i = 0; i < words.size(); i++) { 
      if (strcmp(temp, words[i]) == 0) { 
       already_present = true; 
      } 
     } 

     if (already_present) { 
      delete temp; 
     } else { 
      words.push_back(temp); 
     } 
    } 

    /* Print the desired output */ 
    for(unsigned int i=0; i<words.size(); i++) { 
     cout << words[i] << endl; 
    } 

    return 0; 
} 

有任何疑问,意见深受欢迎。

编辑:在阅读您的意见后,我得出结论,您使用Microsoft Visual Stdio。请参阅,您得到警告的原因是strcpy()可能不安全,因为如果您尝试将字符串复制到不足以包含它的缓冲区,它可能导致缓冲区溢出。

考虑一个代码片段片刻:

char foo[10];  /* a buffer able to hold 9 chars (plus the null) */ 
char bar[] = "A string longer than 9 chars"; 

strcpy(foo, bar); /* compiles ok, but VERY BAD because you have a buffer overflow 
         and are corrupting memory. */ 

strcpy_s()安全,因为你必须明确指定目标缓冲区的大小,所以这个功能不溢出:

strcpy_s(foo, 10, bar); /* strcpy_s will not write more than 10 characters */ 

限制本strcpy_s()是,它是非标准和MS特定的。因此,如果你编写代码来使用它,你的代码将不再是可移植的。

+0

使用了您的建议,但是当我编译时我现在得到错误: 错误\t C4996 \t'strcpy':此函数或变量可能不安全。考虑使用strcpy_s代替。要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。 – sanangelohorn

+0

谢谢你的帮助!我用你的建议,但是我现在得到的错误: – sanangelohorn

+0

@sanangelohorn什么错误? – surajsn