小编GE *_* LO的帖子

C++ type conversion operator

I am studying operator overloading, there are some parts that are difficult to understand.

See this example code.

class A {
    private:
    char a;
    int b;
    double c;

public:
A(char _a = 'a', int _b = 99, double _c = 1.618) :a(_a), b(_b), c(_c){
}

public:
    operator char() const {
        cout << "operator char() called" << endl;
        return this->a;
    }

operator int() const {
        cout << "operator int() called" << endl;
        return this->b;
    }

operator double() {
        cout << "operator …
Run Code Online (Sandbox Code Playgroud)

c++ types casting

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

为什么不是条件 *First1 == * First2 ?? (C++ STL 包括函数实现)

我是一名学习 STL C++ 的学生,有一个简单的问题。

我在学习如何在算法库中实现包含函数时想知道。请参阅此代码(此代码来自https://en.cppreference.com/w/cpp/algorithm/includes

template<class InputIt1, class InputIt2> 
bool includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) {
    for (; first2 != last2; ++first1) {
        if (first1 == last1 || *first2 < *first1)
            return false;
        if ( !(*first1 < *first2) )
            ++first2;
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

本节有问题。

if ( !(*first1 < *first2) )
            ++first2;
Run Code Online (Sandbox Code Playgroud)

这部分表现为判断是否*first1等于的代码,*first2以确定给定的部分序列是否是有序序列的一部分。我认为。

如果是这样,*first1 == * first2似乎足够了,我想知道为什么!(first1 < first2)

这种情况有什么特殊原因吗??

c++ algorithm stl

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

标签 统计

c++ ×2

algorithm ×1

casting ×1

stl ×1

types ×1