标签: std

容器中的智能指针如std :: vector?

我正在学习有关智能指针(std::auto_ptr),只是看这里这里是智能指针(std::auto_ptr)不应该在容器(即放std::vector),因为即使大多数编译器不会抱怨,这似乎是正确的.没有规则说智能指针不会在内部复制(vector例如按类)并传输其所有权,然后指针将变为NULL.最后,一切都会搞砸.

实际上,这种情况多久发生一次?

有时我有指针的向量,如果在将来我决定我想要一个智能指针的矢量我会选择什么?

我知道C++ 0x和Boost库,但是现在,我更倾向于坚持使用STL方法.

c++ pointers smart-pointers std auto-ptr

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

通过隐式转换为字符串流式传输对象时,重载决策失败

免责声明:知道应该避免隐式转换为字符串,并且正确的方法是op<<过载Person.


请考虑以下代码:

#include <string>
#include <ostream>
#include <iostream>

struct NameType {
   operator std::string() { return "wobble"; }
};

struct Person {
   NameType name;
};

int main() {
   std::cout << std::string("bobble");
   std::cout << "wibble";

   Person p;
   std::cout << p.name;
}
Run Code Online (Sandbox Code Playgroud)

在GCC 4.3.4上产生以下结果:

prog.cpp: In function ‘int main()’:
prog.cpp:18: error: no match for ‘operator<<’ in ‘std::cout << p.Person::name’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/ostream:112: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading std implicit-conversion

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

在C++中拆分一系列空分隔字符串的简单方法

我有一系列字符串存储在一个数组中,由空格分隔(例如['f','o','o','\ 0','b','a','r','\ 0'...]),我需要把它分成一个std::vector<std::string>或类似的.

我可以写一个10行循环来使用std::findstrlen(事实上​​我刚才这样做),但我想知道是否有更简单/更优雅的方法来做,例如我忽略的一些STL算法,可以哄骗这样做.

这是一个相当简单的任务,如果有一些聪明的STL技巧可以应用于使其更简单,我也不会感到惊讶.

任何接受者?

c++ stl std

19
推荐指数
3
解决办法
5850
查看次数

什么是std :: condition_variable和std :: condition_variable_any之间的区别?

我可能失去了一些东西很明显,但我看不到之间有什么区别std::condition_variablestd::condition_variable_any.为什么我们两个都需要?

c++ std condition-variable c++11

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

C++标准库中的boost :: optional替代品

我试图让我的程序在没有boost使用的情况下工作,但是找不到某些有用模式的替代方案.也就是说,我boost::optional在标准库中找不到-likewise模式.是否有boost::optional(C++ 11或其他地方)的标准替代方案?

c++ boost std c++11 boost-optional

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

std :: result_of简单函数

#include <iostream>
#include <type_traits>

double f(int i)
{
        return i+0.1;
}

struct F
{
        public:
        double operator ()(int i) { return i+0.1; }
};

int
main(int, char**)
{
        std::result_of<F(int)>::type x;     // ok
        // std::result_of<f(int)>::type x; // error: template argument 1 is invalid
        x = 0.1;
        std::cerr << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

请解释为什么std::result_of<f(int)>::type x;无效......

cppreference说"(std::result_of)在编译类型中扣除函数调用表达式的返回类型."

有什么问题?

c++ std result-of c++11

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

c ++ 11 constexpr将std :: array的列表展平为数组

我开始使用c ++ 11,constexpr和模板元编程似乎是一种在微型微控制器上保存稀缺内存的好方法.

有没有办法编写模板来展平constexpr数组列表,我需要的是一种方法:

constexpr std::array<int, 3> a1 = {1,2,3};
constexpr std::array<int, 2> a2 = {4,5};
constexpr auto a3 = make_flattened_array (a1,a2);
Run Code Online (Sandbox Code Playgroud)

我使用gcc 4.8.4(arm-none-eabi),如果需要,可以使用std = c ++ 11或c ++ 1y选项进行编译.

c++ arrays std constexpr c++11

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

为什么使用std :: make_*而不是构造函数更好?

有些情况下,与启动STL一些功能make_前缀一样std::make_pair,std::make_shared,std::make_unique等它为什么一个更好的做法是使用它们,而不是简单地使用构造?

auto pair2 = std::pair< int, double >( 1, 2.0 );
auto pair3 = std::make_pair( 1, 2.0 );

std::shared_ptr< int > pointer1 = std::shared_ptr< int >( new int( 10 ) );
std::shared_ptr< int > pointer2 = std::make_shared< int >( 10 );
Run Code Online (Sandbox Code Playgroud)
  • 我只是看到这些函数使代码变得更短,但这就是全部吗?
  • 还有其他优点吗?
  • 这些功能更安全吗?

c++ std c++11

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

C++ 17中不推荐使用std :: is_literal_type

根据cppreference,该特性std::is_literal_type在C++ 17中已被弃用.问题是为什么以及什么是未来的首选替代品来检查类型是否是文字类型.

c++ std deprecated type-traits c++17

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

partition_point和lower_bound有什么区别?

C++ 11包括该算法std::partition_point().然而,对于我尝试过的所有情况,它给出了相同的答案std::lower_bound().唯一的区别是方便的T& value参数.

我错过了什么,或者这两个功能或多或少都做了同样的事情?

c++ partitioning std binary-search c++11

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