类模板的参数列表“;std::vector”;缺少

argument list for class template "std::vector" is missing

本文关键字:std 缺少 vector 参数 列表      更新时间:2023-10-16

我需要一些关于这个程序的帮助。我上了我的第一堂编程课,在试图让我的程序正常工作时遇到了麻烦。到目前为止,我已经包含了我所写的内容,但它仍然没有编译。它给出错误: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);
        }
    }
}

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

#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的。因此,如果您编写代码来使用它,那么您的代码将不再是可移植的。