简体   繁体   中英

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

I am in need of some help with this program. I am in my first ever programming class and have run into wall trying to getting my program to work. I have included what I have written so far but still it doesn't compile. It is giving the error: argument list for class template "std::vector" is missing .

Here is the question: When you read a long document, there is a good chance that many words occur multiple times. Instead of storing each word, it may be beneficial to only store unique words, and to represent the document as a vector of pointers to the unique words. Write a program that implements this strategy. Read a word at a time from cin . Keep a vector <char *> of words. If the new word is not present in this vector, allocate memory, copy the word into it, and append a pointer to the new memory. If the word is already present, then append a pointer to the existing word.

Below is code snippet:

#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);
        }
    }
}

I hope below code snippet could be helpful:

#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;
}

Any doubt, comments most welcome.

EDIT: After reading your comments, I came to the conclusion that you use Microsoft Visual Stdio. See, the reason you were getting warning is that strcpy() is potentially unsafe because it can lead to buffer overflow if you try to copy a string to a buffer that is not large enough to contain it.

Consider a code snippet for a moment:

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() is safer because you have to explicitly specify the size of the target buffer, so the function will not overflow:

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

The limitations of this strcpy_s() is that, it is non-standard and MS specific. Therefore if you write code to use it, your code will not be portable any more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM