小编Iam*_*non的帖子

将一对插入std :: vector时emplace_back()vs push_back

我定义了以下内容

std::vector<std::pair<int,int> > my_vec;
my_vec.push_back( {1,2} ); //this works
my_vec.emplace_back( {1,2} ); // this doesn't work
std::pair<int,int> temp_pair = {1,2}; 
my_vec.emplace_back( temp_pair );         //this works
Run Code Online (Sandbox Code Playgroud)

我正在使用c ++ 11进行编译。第三行是有问题的,但是我认为您可以emplace_back()在任何地方使用push_back(),但这显然是错误的。为什么第三行不起作用?

c++ stl push c++11 emplace

3
推荐指数
2
解决办法
462
查看次数

为什么以下代码使用 clang 而不是 gcc 编译

#include <iostream>
#include <unordered_map>
#include <string>

struct tree_node {
  // tree_node() : attrib_val{"null"} {}
  std::unordered_map<std::string, tree_node> child;
};
int main(int argc, char const *argv[])
{
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码在我的 mac 上用 clang 编译得很好:

$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

$ g++ -std=c++11 test.cpp
$ 
Run Code Online (Sandbox Code Playgroud)

在我的 linux 机器上,使用 gcc 9.1.0,我收到以下错误:

In file included from /usr/um/gcc-9.1.0/include/c++/9.1.0/bits/stl_algobase.h:64,
                 from /usr/um/gcc-9.1.0/include/c++/9.1.0/bits/char_traits.h:39,
                 from /usr/um/gcc-9.1.0/include/c++/9.1.0/ios:40,
                 from /usr/um/gcc-9.1.0/include/c++/9.1.0/ostream:38,
                 from /usr/um/gcc-9.1.0/include/c++/9.1.0/iostream:39,
                 from test.cpp:1:
/usr/um/gcc-9.1.0/include/c++/9.1.0/bits/stl_pair.h: In instantiation …
Run Code Online (Sandbox Code Playgroud)

c++ gcc unordered-map clang

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

从 STL 堆栈/队列的顶部/前面移动是否安全?

这个问题对堆栈和队列都适用,但为了简单起见,我在这里只提到一个堆栈。

假设我们将非常量对象推入std::stack,当我们从堆栈中弹出时,在弹出之前将堆栈顶部的对象移动到一个临时变量中是否安全,如下所示:

std::stack<std::string> st;
st.emplace("asdf");
auto popped = std::move(st.top());
st.pop(); 
Run Code Online (Sandbox Code Playgroud)

c++ stack move

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

错误:无法将默认模板参数添加到类模板成员的定义中

当我使用 c++11 编译以下程序时,出现错误error: cannot add a default template argument to the definition of a member of a class template。为什么编译器不允许您在成员函数定义中使用默认模板参数?

#include <iostream>

template<typename A, typename T = int>
class my_class
{
private:
  void dosomething();
};

template<typename A, typename T = int>
void my_class<A, T>::dosomething()
{

}

int main( ) 
{ 
  B<int> obj;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates default

2
推荐指数
1
解决办法
1574
查看次数

右值引用上的 std::vector&lt;T&gt;::emplace_back

我在一个类中有以下功能

  void add_state(std::string &&st) {
    state.emplace_back(st);           // state is a vector
  }
Run Code Online (Sandbox Code Playgroud)

st是基于我的理解的左值(在本例中为字符串的右值引用)。如果,我想st进入 中的最后一个元素state,我应该使用state.emplace_back(std::move(st))吗?如果我按照上面写的方式保留它会发生什么?

编辑 1(如何add_state调用的示例):

// do a series of operations to acquire std::string str
add_state(std::move(str)); 
// also note that str will never be used again after passing it into add_state
Run Code Online (Sandbox Code Playgroud)

如果我add_state(std::string &st)代替它会更好吗?在这种情况下,我想我可以简单地用add_state(str)?

c++ emplace

2
推荐指数
1
解决办法
216
查看次数

如何将 std::variant&lt;int, string&gt; 转换为字符串

我有一个std::variant<int, std::string>。不管它代表的是int还是字符串,我都想将它转换为字符串。int从到 的转换string很简单std::to_string(int)

目前,我使用:


get_if<int> int_ptr{&v};  // v is a std::variant<int, std::string> and we don't know before hand which it is

string res;
if (int_ptr == nullptr) {
  res = *get_if<string>{&v};
} else {
  res = to_string(*int_ptr);
}

Run Code Online (Sandbox Code Playgroud)

有没有更短的方法?

c++ string variant

2
推荐指数
2
解决办法
184
查看次数

'max' 没有重载匹配 'std::function&lt;const int &amp;(const int &amp;, const int &amp;)&gt;'

此代码无法编译,并给出错误:

test.cpp:12:4: error: no matching function for call to 'func'
   func(std::max<int>);
   ^~~~
test.cpp:4:6: note: candidate function not viable: no overload of 'max' matching 'std::function<const int &(const int &, const int &)>' for 1st argument
void func(std::function<const int &(const int &, const int &)> a)
     ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
#include<iostream>
#include<functional>
#include <algorithm>

void func(std::function<const int &(const int &, const int &)> a)
// void func(const int &a(const int &, const int &))       //this one works
{
   std::cout << a(4,5) << …
Run Code Online (Sandbox Code Playgroud)

c++ function

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

C++中STL向量的增长因子是常数还是自适应?

标准是否指定std::vector的增长向量必须是恒定的,还是可以自适应?

在我的 linux 和 mac 机器上,我相信它总是不变的(凭经验观察——但我没有检查这是否以非常大的尺寸维护)。2x 后者,我不记得前者。

c++ vector

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

标签 统计

c++ ×8

emplace ×2

c++11 ×1

clang ×1

default ×1

function ×1

gcc ×1

move ×1

push ×1

stack ×1

stl ×1

string ×1

templates ×1

unordered-map ×1

variant ×1

vector ×1