假设我们有
#include <chrono>
#include <iostream>
#include <ctime>
namespace Ratios { typedef std::ratio<60*60*24,1> Days; }
typedef std::chrono::system_clock Clock;
typedef Clock::time_point TimePoint;
Run Code Online (Sandbox Code Playgroud)
我们main
看起来像
int main(int argc, char *argv[])
{
// argc check left out for brevity
const Clock::rep d = static_cast<Clock::rep>(std::atoi(argv[1]));
// Right now
TimePoint now = Clock::now();
// Start with zero days
auto days = std::chrono::duration<Clock::rep, Ratios::Days>::zero();
// Now we'd like to add d to the days
days += d; // Error!
days.count() = d; // Error!
days = …
Run Code Online (Sandbox Code Playgroud) 为什么不std::runtime_error
提供构造函数接受std::string&&
?看一下构造函数std::string
,它有一个移动构造函数,但noexcept
规范仅适用于C++ 14,而不是C++ 11.这是一个错误,错过了截止日期还是我错过了什么?
使用C++ 11 std::find
而不是容器的find
方法有什么好处吗?
在std::vector
(没有find
方法)的情况下,确实std::find
使用一些智能算法或简单地迭代每个元素的天真方式?
在这种情况下,std::map
你似乎需要传递一个std::pair
,这是value_type
一个std::map
.这通常不是很有用,因为通常你想找到一个键或一个映射元素.
那些像std::list
或std::set
或其他容器怎么样std::unordered_set
?
我正在研究多个主题分支。一个主题分支有很多新功能,并且比主分支领先 17 次提交。
现在我决定研究另一个主题,并转向一个新主题。我进行了一次提交,并且该提交已准备好在 GitHub 上进行拉取请求。然而我注意到我不小心分支出了我之前的主题分支而不是主分支,因此 GitHub 正在预览我与另一个主题分支的其他 17 个提交。如何将此新提交移动到没有前一个主题分支的提交的主题分支?
正如标题所示:是否有可能确保在编译时最多调用一次constexpr函数?
如果功能不是constepxr,这显然是不可能的; 每当我按空格键时,我都可以编写一个被调用的函数,因此编译器永远无法在编译时解决这个问题.
假设我们有一个 CMake 函数func
,它接受命名参数,比如NAMED1
和NAMED2
。第一个参数是强制性的,第二个是可选的。例如
func(NAMED1 foo NAMED2 optional)
Run Code Online (Sandbox Code Playgroud)
现在我有一个循环需要func
在每次迭代时调用这个函数,但根据当前的迭代,我有时需要提供可选值,有时不需要。所以在每次迭代时,我都会构建一个参数列表,比如
list(APPEND args "NAMED1" "foo")
if (...something...)
list(APPEND args "NAMED2" "optional")
endif()
Run Code Online (Sandbox Code Playgroud)
所以我args
现在有了清单。作为一个字符串,它可能看起来像
NAMED1;foo
Run Code Online (Sandbox Code Playgroud)
或者它可能看起来像
NAMED1;foo;NAMED2;optional
Run Code Online (Sandbox Code Playgroud)
所以我想用字符串替换整个列表
string(REPLACE ";" " " args "${args}")
Run Code Online (Sandbox Code Playgroud)
然后结果args
看起来像
NAMED1 foo NAMED2 optional
Run Code Online (Sandbox Code Playgroud)
所以我想把这个字符串传递给函数func
,用
func("${args}")
Run Code Online (Sandbox Code Playgroud)
但现在的问题是 CMake 认为这是一个大字符串,而它应该将其解释为单独的字符串。我怎样才能做到这一点?
所以,我制作了自己的"标准兼容"容器.我定义了被调用的嵌套类iterator
,const_iterator
它来源于
std::iterator<std::bidirectional_iterator_tag, value_type>
Run Code Online (Sandbox Code Playgroud)
value_type
我的新容器类中的typedef在哪里.我来自于std::iterator
我可以很容易地做到
typedef typename std::iterator<std::bidirectional_iterator_tag,value_type>::value_type value_type;
typedef typename std::iterator<std::bidirectional_iterator_tag,value_type>::difference_type difference_type;
// ... etcetera
Run Code Online (Sandbox Code Playgroud)
在我的嵌套迭代器类中.但是,cppreference.com表示std::iterator
没有用于const_reference的typedef,也没有用于const_pointer的typedef.我可以自己输入dede,但我很困惑为什么省略这些typedef.
我有很多类类型(源自type
)。当我打印它们时,我得到类似的东西:
...
<class 'Default.new_templates.NewSnippetCommand'>
<class 'Default.new_templates.NewSyntaxCommand'>
<class 'Default.pane.ClosePaneCommand'>
<class 'Default.pane.FocusNeighboringGroup'>
<class 'Default.pane.MoveToNeighboringGroup'>
<class 'Default.pane.NewPaneCommand'>
<class 'Default.pane.SetMaxColumns'>
...
Run Code Online (Sandbox Code Playgroud)
我想打印:
...
NewSnippetCommand
NewSyntaxCommand
ClosePaneCommand
FocusNeighboringGroup
MoveToNeighboringGroup
NewPaneCommand
SetMaxColumns
...
Run Code Online (Sandbox Code Playgroud)
如何获取类名部分并省略模块部分?
好吧,所以我们知道STL中的功能就像
std::fill(boolContainer.begin(), boolContainer.end(), false);
Run Code Online (Sandbox Code Playgroud)
我正在使用一个也适用于容器的方法开发一个类,我意识到我也可以像上面的例子那样模板化.非模板化的版本是这样的:
class SomeClass {
public:
// ...
int containerMethod(std::vector<int> &v);
// ...
private:
// ...
};
Run Code Online (Sandbox Code Playgroud)
我的目标是将其改为:
class SomeClass {
public:
// ...
template <class InputIterator>
int containerMethod(const InputIterator &begin, const InputIterator &end);
// ...
private:
// ...
};
Run Code Online (Sandbox Code Playgroud)
但是我在解决实施的细节方面遇到了麻烦:
template <class Iter> int SomeClass::containerMethod
(const Iter &begin, const Iter&end) {
// Here I need to instantiate an iterator for the container.
Iter iter;
for (iter = begin; iter != end; ++iter) {
// This does …
Run Code Online (Sandbox Code Playgroud)