对于C++STL中的const_iterator和非const迭代器有什么区别?感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解constiterator和const_iterator,并且
对于C++ STL 中的 const_iterator 和非 const 迭代器有什么区别?感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解const iterator和const_iterator,并且为您提供关于Angular的constructor和ngOnInit里写代码有什么区别?、c – constexpr结束istream(sentinel)迭代器有什么意义?、c – static const和constexpr变量之间有什么区别?、c – STL const_iterator cast – 编译器差异的宝贵知识。
本文目录一览:- C++ STL 中的 const_iterator 和非 const 迭代器有什么区别?(const iterator和const_iterator)
- Angular的constructor和ngOnInit里写代码有什么区别?
- c – constexpr结束istream(sentinel)迭代器有什么意义?
- c – static const和constexpr变量之间有什么区别?
- c – STL const_iterator cast – 编译器差异
C++ STL 中的 const_iterator 和非 const 迭代器有什么区别?(const iterator和const_iterator)
const_iterator
a和 an有什么区别,iterator
你会在哪里使用一个而不是另一个?
答案1
小编典典const_iterator
s 不允许您更改它们指向的值,常规iterator
s 可以。
与 C++ 中的所有内容一样,始终喜欢const
,除非有充分的理由使用常规迭代器(即,您想使用它们不会const
更改指向值的事实)。
Angular的constructor和ngOnInit里写代码有什么区别?
参考这个StackOverflow讨论Difference between Constructor and ngOnInit
得赞超过1000的答案:
The Constructor is a default method of the class that is executed when the class is instantiated and ensures proper initialisation of fields in the class and its subclasses. Angular, or better Dependency Injector (DI), analyses the constructor parameters and when it creates a new instance by calling new MyClass() it tries to find providers that match the types of the constructor parameters, resolves them and passes them to the constructor like
当class被实例化时,constructor是默认被执行的方法,确保类和其子类都被正确地初始化。Angular依赖注入机制,会分析constructor的输入参数,当使用new MyClass创建class实例时,会试着去查找能匹配构造函数类型的providers,解析providers并将结果传递到类的构造函数里。
ngOnInit is a life cycle hook called by Angular to indicate that Angular is done creating the component.
ngOnInit是一个生命周期钩子,Angular调用ngOnInit时,向应用程序传递这样一个信息:Angular已经完成了Component的创建工作。
We have to import OnInit like this in order to use it (actually implementing OnInit is not mandatory but considered good practice):
import { Component, OnInit } from ''@angular/core'';
ngOnInit的使用并不是毫无代价的,得需要导入OnInit,然后实现这个hook:
export class App implements OnInit {
constructor() {
// Called first time before the ngOnInit()
}
ngOnInit() {
// Called after the constructor and called after the first ngOnChanges()
}
}
Implement this interface to execute custom initialization logic after your directive''s data-bound properties have been initialized. ngOnInit is called right after the directive''s data-bound properties have been checked for the first time, and before any of its children have been checked. It is invoked only once when the directive is instantiated.Mostly we use ngOnInit for all the initialization/declaration and avoid stuff to work in the constructor. The constructor should only be used to initialize class members but shouldn''t do actual "work".
So you should use constructor() to setup Dependency Injection and not much else. ngOnInit() is better place to "start" - it''s where/when components'' bindings are resolved.
最佳实践
constructor只用于setup依赖注入,以及初始化类的成员。其他所有业务相关的自定义初始化逻辑,均放在ngOnInit hook里完成。
c – constexpr结束istream(sentinel)迭代器有什么意义?
istream_iterator
andistreambuf_iterator
should support literal
sentinel values. The default constructor is frequently used to
terminate ranges,and Could easily be a literal value for
istreambuf_iterator
,andistream_iterator
when iterating value
types. [Rest omitted]
这对我来说并没有什么意义.有人能告诉我一个他们的意思的例子吗?
N3308是另一篇提到但并不解释问题的论文:
Some of the
istream_iterator<T>
constructors are required to be
constexpr
ifT
is a literal type. The intention is to allow
existing implementation technique of storing a type ofT
inline to
continue to work. [libstdc++ does this,_Tp _M_value
] However,it
actually rules out this technique: the default and copy constructors
ofT
need not be markedconstexpr
,and if they are not,the
istream_iterator<T>
constructors Could not be instantiated as
constexpr
.
以上解释了这个琐碎的拷贝构造函数和析构函数,但是并不是为什么默认构造函数被标记为constexpr.
此外,在线GCC 5.2.0的测试中,我复制了libstdc的实现.唯一的改变是我从istream_iterator()中删除了constexpr.在这两种情况下,组件是相同的.
With constexpr
Without constexpr
解决方法
// istream_iterator example #include <iostream> // std::cin,std::cout #include <iterator> // std::istream_iterator int main () { double value1,value2; std::cout << "Please,insert two values: "; std::istream_iterator<double> eos; // end-of-stream iterator std::istream_iterator<double> iit (std::cin); // stdin iterator if (iit!=eos) value1=*iit; ++iit; if (iit!=eos) value2=*iit; std::cout << value1 << "*" << value2 << "=" << (value1*value2) << '\n'; return 0; }
http://www.cplusplus.com/reference/iterator/istream_iterator/istream_iterator/
声明这个一个constexpr允许编译器将创建流终端迭代器的调用折叠成常量,而不是每次调用一个函数.否则可能会在循环的每次迭代中这样做.
c – static const和constexpr变量之间有什么区别?
例如,对于模板或静态asser.
但如果我想在没有constexpr的情况下这样做,我可以使用static const.
什么是自C 11/14引入constexpr之间的区别
constexpr int a = 3; //AND static const int a = 3;
谢谢!
另一种看待这个问题的方法是我应该使用哪种方法?
解决方法
const static int x = rand();
c – STL const_iterator cast – 编译器差异
error C2440: ” : cannot convert from ‘unsigned char *’ to ‘std::_Vector_const_iterator>>’
class foo { unsigned char* value; int length; std::vector<unsigned char>::const_iterator begin(); std::vector<unsigned char>::const_iterator end(); }; std::vector<unsigned char>::const_iterator foo::begin() { return std::vector<unsigned char>::const_iterator(value); } std::vector<unsigned char>::const_iterator foo::end() { return std::vector<unsigned char>::const_iterator(value + length); }
鉴于我不想重写整个事物,是否有可行的方法来创建这些const_iterators?
解决方法
最简单的解决方案是用unsigned char const *替换std :: vector< unsigned char> :: const_iterator.原始指针属于RandomAccessIterator类别,与vector ::(const_)迭代器相同.
unsigned char const *foo::begin() { return value; } unsigned char const *foo::end() { return value + length; }
如果您需要将迭代器作为类类型,那么您将需要创建自定义迭代器.虽然这可以从头开始,但使用Boost.IteratorFacade要容易得多,这将提供构建自定义迭代器的一堆必要的样板.
#include <boost/iterator/iterator_facade.hpp> struct const_foo_iterator : boost::iterator_facade<const_foo_iterator,unsigned char const,boost::random_access_traversal_tag> { const_foo_iterator() = default; const_foo_iterator(unsigned char const *iter) : iter(iter) {} private: friend class boost::iterator_core_access; void increment() { ++iter; } void decrement() { --iter; } void advance(std::ptrdiff_t n) { iter += n; } std::ptrdiff_t distance_to(const_foo_iterator const& other) const { return iter - other.iter; } bool equal(const_foo_iterator const& other) const { return this->iter == other.iter; } unsigned char const& dereference() const { return *iter; } unsigned char const* iter = nullptr; }; const_foo_iterator foo::begin() { return value; } const_foo_iterator foo::end() { return value + length; } static_assert(std::is_same<std::iterator_traits<const_foo_iterator>::value_type,unsigned char>::value,"value_type"); static_assert(std::is_same<std::iterator_traits<const_foo_iterator>::pointer,unsigned char const *>::value,"pointer"); static_assert(std::is_same<std::iterator_traits<const_foo_iterator>::iterator_category,std::random_access_iterator_tag>::value,"iterator_category");
Live demo
关于C++ STL 中的 const_iterator 和非 const 迭代器有什么区别?和const iterator和const_iterator的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Angular的constructor和ngOnInit里写代码有什么区别?、c – constexpr结束istream(sentinel)迭代器有什么意义?、c – static const和constexpr变量之间有什么区别?、c – STL const_iterator cast – 编译器差异的相关知识,请在本站寻找。
本文标签: