类模板的参数列表“;std::vector”;缺少
argument list for class template "std::vector" is missing
我需要一些关于这个程序的帮助。我上了我的第一堂编程课,在试图让我的程序正常工作时遇到了麻烦。到目前为止,我已经包含了我所写的内容,但它仍然没有编译。它给出错误: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的。因此,如果您编写代码来使用它,那么您的代码将不再是可移植的。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- 为什么 std::span 缺少 cbegin 和 cend 方法?
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- 如何为缺少预定义运算符而不扩展命名空间"std"的标准类型定义运算符>> (istream &, ...)?
- 我在使用 std::fixed 时缺少什么
- 在Visual Studio 2017中,有没有一种快速的方法可以将"std::"添加到缺少它的名称中?
- std::vector for_each error C3867函数调用缺少参数列表
- 将const char*转换为std::string时缺少数据
- cilk_for 通过 std::set 缺少运算符-() 进行并行化的错误
- 类模板的参数列表“;std::vector”;缺少
- Std::next_permutation缺少一个表项
- Std::list抱怨缺少第二个模板参数(allocator)
- 缺少一元std::copy的最佳实现
- 使用MXE gcc构建Qt应用程序时缺少std::互斥锁