小编JeJ*_*eJo的帖子

可以在本地使用lambda吗?

是否可以将lambda函数定义为仅在本地使用,即在当前块(函数/方法)中?考虑一些代码将要执行多次的情况(因此将它放入函数中是合乎逻辑的)但它永远不会在块之外使用.

void foo() {
    auto bar = []() {
        // some code applicable only inside foo()
    };

    bar();
    bar();
    bar();
}
Run Code Online (Sandbox Code Playgroud)

bar()声明为正常功能相比,这种方法有哪些优点和缺点?

c++ lambda function code-cleanup

8
推荐指数
4
解决办法
1257
查看次数

如何获得两个不同类型相乘的结果类型?

如何获得两种不同类型的乘积的结果类型,即

template< typename TA, typename TB>
struct multiplier
{
    using result_type = // something here finds result of TA * TB
    result_type operator()( TA a, TB b ) const
    {
        return a * b;
    }
};
Run Code Online (Sandbox Code Playgroud)

我知道在C ++中将两个不同类型的数值相乘是完全有效的,这将给出编译器已知的类型的值。即将a double和an 相乘int将得到double类型答案。

这样,在编译时知道类型的模板类中,应该可以确定将要创建的类型。实际上,可以创建一个lambda来返回该值的结果,即

auto foo = [](int a, float b){ return a * b;}
auto c = foo( 13, 42.0 );
Run Code Online (Sandbox Code Playgroud)

这将导致c成为float

请注意,我仅限于只能使用或更低版本的功能。

c++ templates class return-type c++11

8
推荐指数
3
解决办法
144
查看次数

删除 std::vector&lt;std::string&gt; 中与另一个给定 std::string 中的字符匹配的元素

如何删除向量alphabets中与字符串中的任何字符匹配的元素plaintext

这是我的尝试:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int main()
{
   //init
   std::vector<std::string> alphabets{ "a", "b", "c", "d", "e", "f", "g", "h", "i", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
   //input
   std::string plaintext;
   std::cout << "enter plain text: ";
   std::cin >> plaintext;

   for (std::string::iterator it = plaintext.begin(); it != plaintext.end(); it++)
   {
      std::vector<std::string>::iterator toErase;
      toErase = std::find(alphabets.begin(), alphabets.end(), *it);
      if (toErase != alphabets.end())
      { …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm stdstring stdvector c++-standard-library

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

C++14 中 std::intializer_list 对象的预期生命周期是多少?

请考虑这个简化的程序:

#include <iostream>

struct A
{
    A() { std::cout << "A() "; }
    ~A() { std::cout << "~A() "; }
};

int main()
{
    auto l = std::initializer_list<A>{A()};
    std::cout << ". ";
}
Run Code Online (Sandbox Code Playgroud)

https://gcc.godbolt.org/z/1GWvGfxne

GCC在这里打印

A() . ~A()
Run Code Online (Sandbox Code Playgroud)

意思std::initializer_list是在范围结束时被破坏。

叮当印:

A() ~A() . 
Run Code Online (Sandbox Code Playgroud)

std::initializer_list在它建造的那条线上销毁。

两个编译器都在这里正确运行还是其中之一是错误的?

c++ object-lifetime initializer-list language-lawyer c++14

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

将枚举类变量名解析为字符串

考虑一下,我有以下enum课程:

enum class TestEnum
{
   None = 0,
   Foo,
   Bar
};
Run Code Online (Sandbox Code Playgroud)

我想指定ostream operator ( << )这个enum类,所以我可以写:

std::cout << "This is " << TestEnum::Foo;
Run Code Online (Sandbox Code Playgroud)

并得到以下输出This is Foo

我的问题是:
是否有存储枚举“名称说明符”的地方?(即对于enum class TestEnum 来说,它是None,FooBar所以我可以编写一个函数(或最多是函数模板)来指定ostream运算符,如下所示TestEnum

std::ostream& operator<< ( std::ostream& os, TestEnum aEnum ) {
   return std::string( aEnum.name() );
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我是这样做的:

std::ostream& operator<< ( std::ostream& os, TestEnum aEnum ) {
   switch( aEnum ) …
Run Code Online (Sandbox Code Playgroud)

c++ reflection enums c++11 c++17

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

使用最小堆的堆排序算法

当我heapsort使用 a实现时,min-heap它会从最大到最小对数组进行排序。这是heapsortusing所需的输出min-heap吗?排序完成后再次排序以输出最小到最大似乎是多余的,因为它heap本身具有最小到最大的结构。

代码:

#include <iostream>
#include <vector>
#include "random.h"
#include "print.h"
int parent(int i)
{
    return (i - 1) / 2;
}
int left(int i)
{
    if(i == 0)
        return 1;
    else
        return 2*i;
}
int right(int i)
{   if(i == 0)
        return 2;
    else
        return 2*i + 1;
}
void min_heapify(std::vector<int> &A, int i, int heapsize)
{
    int smallest;
    int l = left(i);
    //std::cout << "left = " << …
Run Code Online (Sandbox Code Playgroud)

c++ heapsort

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

是否有一种简单的方法来调用具有默认参数的函数?

这是一个带有默认参数的函数声明:

void func(int a = 1,int b = 1,...,int x = 1)
Run Code Online (Sandbox Code Playgroud)

func(1,1,...,2) 当我只想设置x参数时,如何避免调用,以及使用之前的默认参数设置其余参数?

例如,就像 func(paramx = 2, others = default)

c++ function-parameter default-parameters c++11

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

嵌套在类模板中的类的前向声明是否合法?

这个代码合法C++14/17吗?

template <class T1, class T2 >
class Foo
{
public:
    class sentry;
};

template <class T1,class T2 = int>
class Foo<T1,T2>::sentry
{
public:
    ~sentry() { }
};
Run Code Online (Sandbox Code Playgroud)

它与GCC 4.9.3一起编译,但在GCC 5.3中失败. 在线演示

我如何解决GCC 5.3的问题

c++ templates language-lawyer c++14 c++17

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

有什么方法可以创建以成员函数或成员作为参数的函数?

我有一个这样的功能:

void f(std::ofstream& ostrm)
{
    auto a = Myglobal->getData1();
    ostrm << a;

    auto b = Myglobal->getData2();
    ostrm << b;

    auto c = Myglobal->m_data1;
    ostrm << c;

    auto d = Myglobal->m_data2;
    ostrm << d;

    //...
    auto z = Myglobal->getData1000();
    ostrm << z;
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以创建将成员函数成员分解为代码的函数作为参数?

abc,D和z不同类型的)

c++ templates member member-functions c++17

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

如何计算 N 个有序集的交集?

下面的例子展示了如何计算两个集合的交集。STL 是否提供了不仅可以为 2 组而且可以为N组执行此操作的工具?

#include <iostream>    
#include <algorithm>
#include <vector>

int main()
{
    std::vector<int> v1 = { 1,2,9,3,4,5 };
    std::vector<int> v2 = { 9,4,2,7,4,1 };
    std::vector<int> v(v1.size() + v2.size());
    std::vector<int>::iterator it;

    std::sort(v1.begin(), v1.end());
    std::sort(v2.begin(), v2.end());
    it = std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());

    v.resize(it - v.begin());
    std::cout << "Both sets have " << (v.size()) << " elements in common:\n";
    for (it = v.begin(); it != v.end(); ++it)
    {
        std::cout << *it << ' ';
    }
    std::cout << '\n';

    return …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm c++-standard-library set-intersection c++11

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