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) 在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 ++入门5版中。std::shared_ptr据说第十二章:
p = q;
Run Code Online (Sandbox Code Playgroud)
“ p并且q是shared_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)
为什么sp1和sp2具有相同use_count?上面说过,赋值递减p的参考计数和增量q的计数?
此外,我不知道这种情况下可能是潜在的三分球sp1和sp2比是相同的类型转换为彼此其他:
std::shared_ptr<int> …Run Code Online (Sandbox Code Playgroud)我已经在C ++入门5版中看到了这个例子,它讨论了智能指针。我有课StrBlobPtr,其可以作为一个伴侣StrBlob。
的成员之一StrBlobPtr是deref:
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 ++入门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++ 标准:
聚合是一个数组或类(第 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) I have this text from C++ primer 5th edition. ch 19.6 Union:
Run Code Online (Sandbox Code Playgroud)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 …
我想知道为什么std::map::erase有一个重载返回一个int代表被删除元素的数量;因此,只要元素是唯一的,那么数字就是1或0。在这种情况下,为什么它不返回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) 我有这个类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
std::function我想知道与指向成员函数的指针一起使用时如何声明返回类型的对象:
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}\nRun Code Online (Sandbox Code Playgroud)\n …