是否可以将lambda函数定义为仅在本地使用,即在当前块(函数/方法)中?考虑一些代码将要执行多次的情况(因此将它放入函数中是合乎逻辑的)但它永远不会在块之外使用.
void foo() {
auto bar = []() {
// some code applicable only inside foo()
};
bar();
bar();
bar();
}
Run Code Online (Sandbox Code Playgroud)
与bar()声明为正常功能相比,这种方法有哪些优点和缺点?
如何获得两种不同类型的乘积的结果类型,即
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 ++ 11或更低版本的功能。
如何删除向量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++14程序:
#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在它建造的那条线上销毁。
两个编译器都在这里正确运行还是其中之一是错误的?
考虑一下,我有以下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,Foo和Bar)所以我可以编写一个函数(或最多是函数模板)来指定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) 当我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) 这是一个带有默认参数的函数声明:
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++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的问题?
我有一个这样的功能:
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)
有什么方法可以创建将成员函数或成员分解为代码的函数作为参数?
(a,b,c,D和z不同类型的)
下面的例子展示了如何计算两个集合的交集。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)