小编JeJ*_*eJo的帖子

如何在C ++ 11中返回包含自定义删除器的std :: unique_ptr?

我的应用程序编译器最多只能支持c++11

下面是我的项目的代码,函数get_conn()返回std::unique_ptr自定义删除器(deleter需要两个参数)。我正在使用auto关键字作为返回类型,但是它给出了一个错误,例如if是否用c++11(用编译很好c++14

error: ‘get_conn’ function uses ‘auto’ type specifier without trailing return type
Run Code Online (Sandbox Code Playgroud)

示范代码示例:

#include <iostream>
#include <functional>
#include <memory>
using namespace std;


// Dummy definition of API calls                                                                                                             
int* open_conn (int handle)
{
  return new int;
}
void close_conn (int handle, int *conn)
{}

auto get_conn (int handle)
{
  // API call                                                                                                                                
  int* conn = open_conn (handle);    
  auto delete_conn = [](int *conn, int handle) {
        // API …
Run Code Online (Sandbox Code Playgroud)

c++ lambda return-type unique-ptr c++11

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

How do I add a string to a vector (and subsequently display it)?

I am 4 hours-new to C++ and have hit a brick wall with string vectors. When trying to add multiple strings to a string vector, I keep erroring out. I'd like to use push_back.

I would also like to display this string vector, but I'm not sure how (I know how to display non-vectors). Given that I have not been able to add a string to a vector of strings, I did not attempt to display the vector of …

c++ class stdstring stdvector c++11

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

如何调用操作员模板?

我对如何实例化此模板感到有些困惑。我知道,简单地使用friend会员身份会实现我想要的东西会更容易,但是如果我强迫这样做,该怎么办?我只是想弄清楚。(顺便说一句,我知道这个模板似乎毫无意义),我只想使其编译即可。

#include <iostream>

template <typename T>
inline std::ostream& operator<< (std::ostream& os, const T& date)
{
    os << date.getD() << " " << date.getM() << " " << date.getY() << "\n";
    return os;
}

class Date 
{
private:
    int dd, mm, yy;
public:
    Date(int d, int m, int y) : dd(d), mm(m), yy(y) {}
    int getD() const;
    int getM() const;
    int getY() const;
};

int Date::getD() const {  return dd; }

int Date::getM() const {  return mm; }

int …
Run Code Online (Sandbox Code Playgroud)

c++ templates class operator-overloading c++11

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

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

计算字符串中元音的函数

我写了函数。但老师告诉我,在3 的参数的std::count_if功能,有必要通过拉姆达找出如果这封信是元音

我不知道如何在那里转移它。

unsigned CalculateVowels(const std::string& str)
{
    const std::string& vowels = "aeiouAEIOU";
    unsigned count = std::count_if(str.begin(), str.end(), [](int index) {return str[index] == vowels[index]; })

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

c++ algorithm lambda stdstring c++11

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

将lambda放在类定义中的干净方法

我有一个在本地函数中可以正常工作的代码:

struct Divider
{
public:
    size_t factor;
    size_t next;
};

void foo()
{
    auto cmp = [](const Divider& x, const Divider& y) { return x.next > y.next; };
    std::priority_queue < Divider, std::vector<Divider>, decltype(cmp)> sieve(cmp);
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我现在想将我的sieve变量移到一个类中。我可以写出以下怪物:

class Bar
{
    inline static auto cmp = [](const Divider& x, const Divider& y) { return x.next > y.next; };
    std::priority_queue < Divider, std::vector<Divider>, decltype(cmp)> sieve = std::priority_queue < Divider, std::vector<Divider>, decltype(cmp)>(cmp);
};
Run Code Online (Sandbox Code Playgroud)

有什么方法可以编写这种默认构造而无需两次指定类型?或者只是以一种更清洁的方式。

c++ lambda class class-members c++11

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

模板参数作为函数未命名参数

这个问题继续 非静态数据成员类推导

这是未命名的参数函数,我用它来返回std::string数据类型的表示

struct Boo {};
struct Foo {};

std::string class2str(const double) { return "Floating"; };
std::string class2str(const int) { return "Fixed Point"; };
std::string class2str(const Foo) { return "Class Foo"; };
std::string class2str(const Boo) { return "Class Boo"; };

int main(int argc, char* argv[]) 
{
    int    x_a;
    double x_b;
    Foo    F;
    Boo    B;
    std::cout << "x_a     :" << class2str(x_a) << std::endl;
    std::cout << "x_b     :" << class2str(x_b) << std::endl;
    std::cout << "Foo     :" << class2str(F) << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ templates class function-templates c++17

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

如何重构嵌套for循环?

我有两个类似的功能。这两个函数都包含一个嵌套for循环。我如何结合这两个函数来减少重复的代码。

funcA和之间的唯一区别funcB是循环中的funcB调用。func_2

这两个函数如下所示。

void funcA()
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            func_1();
        }
    }
}

void funcB() 
{
    for (int i = 0; i < size; i++) 
    {
        for (int j = 0; j < size; j++)
        {
            func_1();
            func_2();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ algorithm refactoring for-loop function

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

如何在模板函数中使用不同的结构作为模板参数?

我正在编写这样的模板函数:

template<typename T>
std::string EncodeData(int DataType, T Data, std::string ReadCommandID, std::string& ThisID);
Run Code Online (Sandbox Code Playgroud)

我想使用的类型T是一些不同的结构,例如:

struct A
{
    int A_data;
    bool A_data_2;
    // etc.......
};

struct B
{
    double B_data;
    char B_data_2;
    // etc.......
};
Run Code Online (Sandbox Code Playgroud)

我希望函数可以根据不同的struct传递来访问不同的成员变量T,所以我写了这样的代码:

template<typename T>
std::string EncodeData(int DataType, T Data, std::string ReadCommandID, std::string& ThisID)
{
    if(std::is_same<T, A>{})
    {
        // Do something with Data.A_data and Data.A_data_2
    }
    else if(std::is_same<T, B>{})
    {
        // Do something with Data.B_data and Data.B_data_2
    }
    // other code.
}
Run Code Online (Sandbox Code Playgroud)

并使用它: …

c++ templates struct function-templates c++17

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

如何实现序列容器和关联容器的通用功能?

我正在考虑编写一个既适用于序列容器又适用于关联容器的函数。这就像

 template<class C, class V = typename C::key_type>
 bool has_val(const C& c, const V& v)`
Run Code Online (Sandbox Code Playgroud)

在函数内部,我想

  1. 检查 C 类是否具有 set/map 等容器类的成员函数const_iterator find(key_type) const
  2. 如果它不包含 find(),那么我们使用std::find()像 之类的序列容器std::vector

检查的最佳做法是什么(1)?

如果我上面描述的不是最好的,请告知是否有更好的方法?

(不幸的是,我无法使用宏访问较新的 Folly FOLLY_create_member_invoker,但我确实有FOLLY_CREATE_HAS_MEMBER_FN_TRAITS。不过,我无法成功完成它)

c++ templates std function-templates c++17

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