如何解决为什么仅在某些时候才需要包含标题?
我是 C++ 的新手,我有这个程序(你会注意到它#include
s iostream
但没有别的):
// Prompt from https://www.learncpp.com/cpp-tutorial/dynamically-allocating-arrays/
#include <iostream>
// #include <algorithm> // for std::sort()
int main() {
int numNames{};
std::cout << "How many names? ";
std::cin >> numNames;
std::string *nameList{ new std::string[static_cast<std::size_t>(numNames)]{} };
std::cin.clear();
std::cin.ignore(10000,'\n');
for (int count{ }; count < numNames; ++count) {
std::string name{};
std::cout << "Enter name #" << count + 1 << ": ";
std::getline(std::cin,name);
nameList[count] = name;
}
std::sort(nameList,nameList + numNames);
std::cout << "\nHere's your list,sorted:" << '\n';
for (int index{ }; index < numNames; ++index) {
std::cout << "Name #" << index + 1 << ": " << nameList[index] << '\n';
}
delete[] nameList;
nameList = nullptr;
return 0;
}
它编译并运行,即使 <string>
、<algorithm>
和 <cstddef>
都被使用了(对于 std::string
、std::sort
和 { {1}})。但是,如果我删除 std::size_t
,则不会。为什么是这样?使用 <iostream>
或 std::string
或 sort
是否会导致编译器错误?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。