我知道数组是一个原始类,因此没有内置的方法来检测超出范围的错误.但是,vector类具有内置函数.at(),它可以检测这些错误.通过使用命名空间,任何人都可以通过在访问向量范围之外时抛出错误来重载[]符号以充当.at()函数.我的问题是:为什么这个功能在C++中不是默认的?
编辑:下面是一个伪代码的例子(我相信 - 如果需要,请纠正我)重载向量运算符[]:
Item_Type& operator[](size_t index) { // Verify that the index is legal.
if (index < 0 || index >= num_items) {
throw std::out_of_range
("index to operator[] is out of range");
}
return the_data[index]
}
Run Code Online (Sandbox Code Playgroud)
我相信这个函数可以写入用户定义的命名空间,并且相当容易实现.如果这是真的,为什么它不是默认的?
我刚刚学习了函数指针,并且要声明一个函数指针,你必须*在括号中添加指针名称,以确保它不返回int *.
两者之间有区别吗?
int *p
Run Code Online (Sandbox Code Playgroud)
和
int (*p)
Run Code Online (Sandbox Code Playgroud)
我尝试用cdecl查找,但到目前为止没有运气.cdecl说这是一回事,但没有提供解释.如果我宣布
short (*p)
Run Code Online (Sandbox Code Playgroud)
那会是一个短指针还是一个短指针?将p具有2个字节或4的大小?我知道有类似的问题,但到目前为止我还没有运气.
我想cout在以下简单程序中的一个向量的内容:
#include<iostream>
#include<ios>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
string name;
double median;
int x;
vector<double> numb, quartile1, quartile2, quartile3, quartile4;
cout << "Please start entering the numbers" << endl;
while (cin >> x)
{
numb.push_back(x);
}
int size = numb.size();
sort(numb.begin(), numb.end());
for (int i = 0; i < size; i++)
{
double y = numb[(size / 4) - i];
quartile1.push_back(y);
}
cout << quartile1; // Error here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试编译这个时,我都会收到此错误:
Error …Run Code Online (Sandbox Code Playgroud) 我已经开发了大约两年的网站,但我从未使用scss,sass或更少.它们与使用它们的优点有什么区别?