小编Ita*_*iwa的帖子

Why I cannot create a stream iterator?

I am supposed to create a looks-like stream iterator class so that I can read from the input stream when incrmenting an object of my class.

I've done this:

template<class T>
struct istrm_it{
    istrm_it() = default;
    istrm_it(std::istream& in) :
        in_(in), val_(T()) {
        in_ >> val_;
    }
    T val_;
    std::istream& in_ = std::cin;
    istrm_it& operator++() { in >> val_; return *this; };
    istrm_it& operator++(int) { in_ >> val_; return *this; };
    T operator*() { return val_; }
    bool operator==(const istrm_it& rhs)const …
Run Code Online (Sandbox Code Playgroud)

c++ istream-iterator

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

从迭代器构造的反向迭代器是其先前的迭代器吗?

https://en.cppreference.com/w/cpp/iterator/reverse_iterator上说:

std::reverse_iterator是一个迭代器适配器,可反转给定迭代器的方向。换句话说,当提供了双向迭代器时,将std::reverse_iterator生成一个新的迭代器,该迭代器将从基础双向迭代器定义的序列的末尾移动到开头。

对于由迭代器r构造的反向迭代器i,该关系&*r == &*(i-1)始终为true(只要r是可取消引用的);因此,从一端到最后的迭代器构造的反向迭代器将引用序列中的最后一个元素。

因此,我尝试使用此代码来了解更多信息:

int main() {


    std::deque<int> di{ 1, 1, 2, 3, 5, 8, 13 }; // fibonacci series
    // deque has bi-directional iterators

    std::deque<int>::iterator offEnd = di.end(); // one-past the last element in di
    std::deque<int>::reverse_iterator r(offEnd); // constructing a reverse iterator from an iterator from deque<int> di

    std::cout << &offEnd << " : " /*<< *r */ << std::endl;
    std::cout << &(offEnd - …
Run Code Online (Sandbox Code Playgroud)

c++ reverse-iterator

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

C ++ Primer 5 Edition:计数参考和基础指针

在C ++入门5版中。std::shared_ptr据说第十二章:

p = q;
Run Code Online (Sandbox Code Playgroud)

p并且qshared_ptr可以互相转换的保持指针。如果's'的计数变为0,则减少p's的引用计数并增加q's的计数,删除p's现有的内存p

所以我尝试了这个:

std::shared_ptr<int> sp1 = std::make_shared<int>(10);
decltype(sp1) sp2 = nullptr;

std::cout << sp1.use_count() << std::endl;
std::cout << sp2.use_count() << std::endl;

sp2 = sp1;

std::cout << sp1.use_count() << std::endl;
std::cout << sp2.use_count() << std::endl;
Run Code Online (Sandbox Code Playgroud)

输出:

sp1.use_count(): 1
sp2.use_count(): 0
sp1.use_count(): 2
sp2.use_count(): 2
Run Code Online (Sandbox Code Playgroud)
  • 为什么sp1sp2具有相同use_count?上面说过,赋值递减p的参考计数和增量q的计数?

  • 此外,我不知道这种情况下可能是潜在的三分球sp1sp2比是相同的类型转换为彼此其他:

    std::shared_ptr<int> …
    Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr c++11

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

我可以在同一表达式中多次使用该对象而不安全地对其进行修改吗?

我已经在C ++入门5版中看到了这个例子,它讨论了智能指针。我有课StrBlobPtr,其可以作为一个伴侣StrBlob

的成员之一StrBlobPtrderef

std::string& StrBlobPtr::deref() const
{
    auto p = check(curr, "dereference past end");
    return (*p)[curr]; // (*p) is the vector to which this object points
}
Run Code Online (Sandbox Code Playgroud)

check返回一个std::shared_ptrnull或指向一个对象。

  • 我只想知道我是否可以直接通过调用来制作return语句check

    std::string& StrblobPtr::deref() {
        return (*check(index_, "dereferencing unbound StrblobPtr!"))[index_];
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 那么index_在同一表达式中两次使用它而不修改它是否定义明确?谢谢!

c++ operator-precedence

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

C ++底漆动态数组的初始化程序数量超过了大小

在C ++入门5版中。第十二章动态内存:

如果初始化程序少于元素,则其余元素将被值初始化。如果初始化程序多于给定的大小,则新表达式将失败,并且不会分配任何存储空间。在这种情况下,new抛出bad_array_new_length类型的异常。与bad_alloc一样,此类型在新的标头中定义。

这是关于分配和初始化动态数组的。但是我认为这是不对的:如果初始化器的数量大于动态数组的大小,则这是编译时错误,而不是运行时错误:

auto p = new int[3]{4, 5, 6, 7}; // compile-time error: Too many initializers.
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-arrays

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

聚合与非聚合结构/类

我试图了解聚合类/结构/联合是什么:这是来自 C++ 标准:

聚合是一个数组或类(第 9 条),没有用户提供的构造函数(12.1),没有用于非静态数据成员的大括号或等号初始化器(9.2),没有私有或受保护的非静态数据成员(第 11 条),没有基类(第 10 条),也没有虚函数(10.3)。

所以我写了这个来测试:

struct NonAggregate{
    virtual void f()const&{} // virtual member function makes the struct non-aggregate
};

struct Bar{}; // aggregate
struct X{}; // aggregate

struct Foo : Bar, X{
   // Foo() = default; // if un-comment out makes Foo non-aggregate
   Foo& operator =(Foo const&){return *this;} // ok
   ~Foo(){} // ok
   int x_ = 0; // has an initializer. ? still aggregate?
   Bar b_{}; // has an initializer. still aggregate?

private:
    static …
Run Code Online (Sandbox Code Playgroud)

c++ aggregate-type

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

C++ primer 5th edition. Union and members of class type

I have this text from C++ primer 5th edition. ch 19.6 Union:

class Token {
public:
    Token(): tok(INT), ival{0} { }
    Token(const Token &t): tok(t.tok) { copyUnion(t); }
    Token &operator=(const Token&);
    ~Token() { if (tok == STR) sval.~string(); }
    Token &operator=(const std::string&);
    Token &operator=(char);
    Token &operator=(int);
    Token &operator=(double);
private:
    enum {INT, CHAR, DBL, STR} tok; // discriminant
    union {                         // anonymous union
        char   cval;
        int    ival;
        double dval;
        std::string sval;
    }; // each Token object has an unnamed member of …
Run Code Online (Sandbox Code Playgroud)

c++ union

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

为什么 std::map::erase 返回 int 而不是 bool?

我想知道为什么std::map::erase有一个重载返回一个int代表被删除元素的数量;因此,只要元素是唯一的,那么数字就是10。在这种情况下,为什么它不返回bool而不是返回int

     std::map<std::string, std::size_t> containers{
    {"map", 1}, {"set", 10}, {"map", 5}, {"vector", 4}, {"array", 7}
};

for(auto const& p : containers)
    std::cout << p.first << " " << p.second << '\n';

std::cout << containers.erase("map") << '\n'; // 1
std::cout << containers.erase("map") << '\n'; // 0

for(auto const& p : containers)
    std::cout << p.first << " " << p.second << '\n';
Run Code Online (Sandbox Code Playgroud)

c++ stdmap c++11

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

为什么转换构造函数优于转换运算符?

我有这个类SmallInt应该表示范围内的正整数值0-255- 包括:

struct SmallInt{
    explicit SmallInt(int x = 0) : iVal_( !(x < 0 || x > 255) ? x :
    throw std::runtime_error(std::to_string(x) + ": value outbounds!")){}
    operator int&() { return iVal_; }
    int iVal_;
};

int main(){

    try{
        SmallInt smi(7);
        cout << smi << '\n';
        cout << smi + 5 << '\n'; // 7 + 5 = 12
        cout << smi + 5.88 << '\n'; // 7.0 + 5.88 = 12.88
        smi = 33; // …
Run Code Online (Sandbox Code Playgroud)

c++ explicit-constructor conversion-operator implicit-conversion

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

如何存储从 std::bind 返回的可调用对象作为指向成员函数的指针?

std::function我想知道与指向成员函数的指针一起使用时如何声明返回类型的对象:

\n
int main(){\n\n    void foo(std::string, int);\n    string s = "hi";\n    auto fn = std::bind(foo, s, std::placeholders::_1);\n    fn(5);// ok\n\n    /*auto fn2 = */std::bind(&std::string::empty, std::placeholders::_1); // error if I uncomment /*auto fn2 = */\n\n    std::vector<std::string> vs{"The", "Quick", "Brown", "Fox", "Jumped", "", "Over", "The", "", "Lazy", "Dog"};\n    auto it = std::find_if(vs.cbegin(), vs.cend(),\n    std::bind(&std::string::empty, std::placeholders::_1)); // OK\n    std::cout << "Empty string found at index: " << (it != vs.cend() ? std::distance(vs.cbegin(), it) : -1) << '\\n'; // 5\n\n    std::cout << "\\ndone!\\n";\n}\n
Run Code Online (Sandbox Code Playgroud)\n …

c++ pointer-to-member stdbind c++11

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