小编int*_*tor的帖子

为什么C++没有检测到默认情况下使用[]运算符访问超出范围的向量元素?

我知道数组是一个原始类,因此没有内置的方法来检测超出范围的错误.但是,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)

我相信这个函数可以写入用户定义的命名空间,并且相当容易实现.如果这是真的,为什么它不是默认的?

c++ vector indexoutofboundsexception

1
推荐指数
1
解决办法
881
查看次数

int(*p)和int*p之间有区别吗?

我刚刚学习了函数指针,并且要声明一个函数指针,你必须*在括号中添加指针名称,以确保它不返回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的大小?我知道有类似的问题,但到目前为止我还没有运气.

c++ pointers

1
推荐指数
1
解决办法
246
查看次数

'<<'运算符的C++错误(对cout的向量内容)

我想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)

c++ c++11

-2
推荐指数
1
解决办法
796
查看次数

css,scss,sass和less之间有什么区别?

我已经开发了大约两年的网站,但我从未使用scss,sass或更少.它们与使用它们的优点有什么区别?

html css sass css3 less

-2
推荐指数
1
解决办法
2933
查看次数

标签 统计

c++ ×3

c++11 ×1

css ×1

css3 ×1

html ×1

indexoutofboundsexception ×1

less ×1

pointers ×1

sass ×1

vector ×1