标签: std

使用c ++ 11 stoi,stof和family,boost :: lexical_cast是多余的吗?

boost::lexical_cast现在的冗余度,C++ 11引入stoi,stof和家人,或者是没有任何理由仍然使用它?(除了没有C++ 11编译器)它们是否提供完全相同的功能?

c++ boost stl std c++11

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

emplace_back()的行为与预期不符

我编写了一个简单的程序,可以在标准库容器中就地创建对象.这就是我写的:

#include <vector>
#include <iostream>

class AB
{
public:
   explicit AB(int n);
   AB(const AB& other) = delete;
   AB(AB&& other);
   AB& operator=(const AB& other) = delete;
   AB& operator=(AB&& other) = default;
private:
   int i;
};

AB::AB(int n): i( n )
{
   std::cout << "Object created." << std::endl;
};

AB::AB(AB&& other): i( std::move(other.i) )
{
   std::cout << "Object moved." << std::endl;
};

int main()
{
   std::vector< AB > v;
   v.emplace_back(1);
   v.emplace_back(2);
   v.emplace_back(3);
};
Run Code Online (Sandbox Code Playgroud)

我用g ++(版本4.8.2)编译它.运行输出后,我得到:

Object created.
Object created.
Object moved.
Object …
Run Code Online (Sandbox Code Playgroud)

c++ g++ std c++11

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

在 C++ 中从单个向量创建对向量

我有一个偶数大小的向量,我想将其转换为成对的向量,其中每对始终包含两个元素。我知道我可以使用简单的循环来做到这一点,但我想知道是否有一个很好的标准库工具可以做到这一点?可以假设原始向量始终包含偶数个元素。

例子:

vector<int> origin {1, 2, 3, 4, 5, 6, 7, 8};

vector<pair<int, int>> goal { {1, 2}, {3, 4}, {5, 6}, {7, 8} };
Run Code Online (Sandbox Code Playgroud)

c++ stl std stdvector std-pair

25
推荐指数
3
解决办法
2213
查看次数

std :: bind重载决议

以下代码工作正常

#include <functional>

using namespace std;
using namespace std::placeholders;

class A
{
  int operator()( int i, int j ) { return i - j; }
};

A a;
auto aBind = bind( &A::operator(), ref(a), _2, _1 );
Run Code Online (Sandbox Code Playgroud)

事实并非如此

#include <functional>

using namespace std;
using namespace std::placeholders;

class A
{
  int operator()( int i, int j ) { return i - j; }
  int operator()( int i ) { return -i; }
};

A a;
auto aBind = bind( &A::operator(), ref(a), …
Run Code Online (Sandbox Code Playgroud)

c++ functional-programming std c++11

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

清除矢量会影响其容量吗?

我实例化了一个std::vector foo(1000).

foo.size()现在是1000,foo.capacity()也是1000.

如果我清除矢量foo.clear(),size()现在是0,但是什么是capacity()?标准是否对此有所说明?

c++ size vector std capacity

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

你能将std :: recursive_mutex与std :: condition_variable结合起来吗?

你可以结合std::recursive_mutex使用std::condition_variable,意思是做这样的事情:

std::unique_lock<std::recursive_mutex> lock(some_recursive_mutex)
some_condition_var.wait(lock);
Run Code Online (Sandbox Code Playgroud)

如果不允许,为什么不呢?

我正在使用VC++ 11.

c++ multithreading std c++11 recursive-mutex

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

std :: set中索引处的元素?

我偶然发现了这个问题:我似乎无法在正常的索引位置选择项目std::set.这是性病的一个错误吗?

下面一个简单的例子:

#include <iostream>
#include <set>

int main()
{
    std::set<int> my_set;
    my_set.insert(0x4A);
    my_set.insert(0x4F);
    my_set.insert(0x4B);
    my_set.insert(0x45);

    for (std::set<int>::iterator it=my_set.begin(); it!=my_set.end(); ++it)
        std::cout << ' ' << char(*it);  // ups the ordering

    //int x = my_set[0]; // this causes a crash!
}
Run Code Online (Sandbox Code Playgroud)

我能做些什么来解决这个问题?

c++ std set

24
推荐指数
4
解决办法
7万
查看次数

std::vector::empty 和 std::empty 之间的区别

要检查向量是否v为空,我可以使用std::empty(v)or v.empty()。我查看了 cppreference 上的签名,但缺乏理解它们的知识。他们之间的关系如何?一个实现是否调用另一个实现?

我知道一个来自容器库,另一个来自迭代器库,但仅此而已。

c++ std

24
推荐指数
4
解决办法
2438
查看次数

静态std :: map出现奇怪的链接器错误

当我尝试在Visual Studio 2008中编译时,为什么会出现链接器错误

#include <stdafx.h>
#include <iostream>
#include <map>
#include <string>

class MyClass
{
public:
 MyClass () { };
 virtual ~MyClass() {};

 static std::string niceString (std::map<int, int> mappp) { _myMap = mappp; return "nice string"; };

private:
 static std::map<int, int> getMap ( ) { return _myMap; };

 static std::map<int, int> _myMap;
}; 

int main(){

 std::map<int, int> mappp;

 mappp[1] = 1;

 std::cout << MyClass::niceString(mappp);

}
Run Code Online (Sandbox Code Playgroud)

错误是:

Error 1 error LNK2001: unresolved external symbol "private: static class std::map<int,int,struct std::less<int>,class std::allocator<struct std::pair<int const …
Run Code Online (Sandbox Code Playgroud)

c++ static std map

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

如果它是一个函数,std :: endl如何不使用任何括号?

问题几乎在标题中.根据C++ Reference,std::endl实际上是一个函数.看看它的声明<iostream>,这可以得到验证.

但是,当您使用时std::endl,您不使用std::endl().相反,你使用:

std::cout << "Foo" << std::endl;
Run Code Online (Sandbox Code Playgroud)

实际上,如果您使用std::endl(),编译器需要更多参数,如上面的链接所示.

有人会解释这个吗?有什么特别之处std::endl?我们可以在调用时实现不需要任何括号的函数吗?

c++ std visual-studio-2010

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