std :: apply在很少的stackoverflow答案和n3658,n3915中提到,通常定义为:
template <typename F, typename Tuple, size_t... I>
decltype(auto) apply_impl(F&& f, Tuple&& t, index_sequence<I...>) {
return forward<F>(f)(get<I>(forward<Tuple>(t))...);
}
template <typename F, typename Tuple>
decltype(auto) apply(F&& f, Tuple&& t) {
using Indices = make_index_sequence<tuple_size<decay_t<Tuple>>::value>;
return apply_impl(forward<F>(f), forward<Tuple>(t), Indices{});
}
Run Code Online (Sandbox Code Playgroud)
但是,参考实现std :: apply函数无法在此类上下文中进行编译(使用clang 3.8和gcc 5.2进行测试):
std::apply ([] (int&) {} , std::make_tuple (42));
Run Code Online (Sandbox Code Playgroud)
一种可能的解决方法是简单地从apply_impl中删除std :: forward <Tuple>,但保持通用引用不变:
template <typename F, typename Tuple, size_t... I>
decltype(auto) apply_impl(F&& f, Tuple&& t, index_sequence<I...>) {
return forward<F>(f)(get<I>(t)...);
}
Run Code Online (Sandbox Code Playgroud)
这种解决方法有任何缺点吗?有更方便的解决方案吗? …
我有一个文本文件,首先描述一些行,然后描述一些彩色线:
1 2 3 4
5 6 7 8
9 10 11 12
red 1 0 0 1 2 3 4
green 0 1 0 5 6 7 8
blue 0 0 1 9 10 11 12
Run Code Online (Sandbox Code Playgroud)
每个部分中的行数在执行时是未知的
我std::cin >>为这些结构重载了运算符:
struct Point { int x, y; }
struct Line { Point a, b; }
struct Color { float r, g, b; std::string name; };
struct ColorfulLine { Line line; Color color; };
Run Code Online (Sandbox Code Playgroud)
(这里的完整示例:http://ideone.com/bMOaL1 …
我想做这样的事情:
std::set<my_type*> s;
s.insert(new my_type(...));
...
s.erase(...);
Run Code Online (Sandbox Code Playgroud)
set的擦除将删除指针以避免内存泄漏.
这是可能的C++容器或适当的解决方案是子类化容器和编写我自己的擦除,或使用某种智能指针方案?
我的C++代码如下:
#include <iostream>
using namespace std;
int main() {
int i = 0;
cout << (i=0) << endl;
if(i=0) {
i=1;
}
cout << i;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么(i=0)等于0?
#include<iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin" << cin;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在这里看到了这个问题:http : //quiz.geeksforgeeks.org/c-misc-c-question-8/ 他们给出的输出是:cin + junk。
但是我不明白它是怎么来的?请解释。
我是C的新人
这是我的代码
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char time[20];
scanf("%s",time);
// command and "hello" can be less than, equal or greater than!
// thus, strcmp return 3 possible values
if (strcmp(time, "PM") == 0)
{
printf("It's PM \n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
假设我在下午12:13:14输入
我想知道它是上午还是下午.但上面的代码只发现整个char数组是否为"PM".我看过其他帖子,我无法理解它们.
我正在编写一个程序,用户可以在开始时设置选项标志.
我的问题是我想根据用户标志调用不同版本的函数,我不想if(flag)每次选择调用哪个版本时都要这样做,因为我必须检查if()我处理的每一行的语句.
这是一个学校项目所以我试图找到最有效的方法来确定我想要调用哪个函数, and I readif()`语句在这里很昂贵.
那么有没有办法在开始时基本上说
if(flag)
// use this function the rest of the program
else
// use this other function for the rest of the program
Run Code Online (Sandbox Code Playgroud) class A{
string m_name;
int m_num;
public:
A(string name="", int number=0) : m_name(name), m_num(number)
{ cout << "ctorA " << m_name << endl; }
virtual ~A(){ cout << "dtorA " << m_name << endl; }
string getName(){ return m_name; }
void setName(const string name){ m_name = name; }
int getNumber(){ return m_num; }
};
class B : public A{
string m_s;
public:
B(string name="", int number=0, string s="")
: A(name, number){ m_s = s; }
string getS(){ return m_s; } …Run Code Online (Sandbox Code Playgroud) 我碰到了basic.scope.namespace#1,并且在我的脑海里有些不清楚:
namespace Foo {
int a;
}
namespace Foo {
// Foo::a = 2; // Why can't do an assignment here?
void b() {
Foo::a = 2; // OK
}
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT
error: ‘a’ does not name a type
Run Code Online (Sandbox Code Playgroud)
我很好奇为什么我们不能对命名空间范围进行分配,
虽然功能块内的赋值是可以的吗?
有任何想法吗?
我想知道当我们在循环中声明一个函数时会发生什么,比如x次运行.例如,
int main() {
for(int i=0;i<100;i++)
{
void my_func(){
cout<<"Hello! Brother"<<endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)