我想使用带向量的通用引用.
template<typename T>
void foo(T&& v)
{
for(typename T::iterator i = v.begin(); i != v.end(); i++)
{
std::cout << *i << std::endl;
}
}
int main()
{
std::vector v = {0,5,4,3};
foo(std::move(v));
foo(v); //compiler error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用foo函数v的参数(没有std :: move)时会导致编译错误.
我认为在这两种情况下,通用引用都应该有效.
错误:
prog.cc: In instantiation of 'void foo(T&&) [with T = std::vector<int, std::allocator<int> >&]':
prog.cc:25:10: required from here prog.cc:16:30: error: 'std::vector<int, std::allocator<int> >&' is not a class, struct, or union type 16 | for(typename T::iterator i = v.begin(); i …Run Code Online (Sandbox Code Playgroud) 我创建了一个以下程序来制作php中的fibanocci系列
function find_max_paths_fib($spaces) {
$c = array();
$c[0] = 1;
$c[1] = 1;
for ($i = 2; $i <= $spaces; $i++) {
if ($i >= 2) {
echo $c[$i] = $c[$i-2] + $c[$i-1];
}
}
return $c[$spaces];
}
Run Code Online (Sandbox Code Playgroud)
$ spaces表示我需要多少生成系列的数字,但是find_max_paths_fib(8000)或者对于一些大数字返回INF,我已经在c ++中尝试过并得到了相同的结果.有没有办法计算它?还是我的功能错了?
编辑:
显然,GCC允许实例化没有参数列表的类模板(当参数默认时),这是不合规的(Clang是合规的).
我猜测要求括号的原因(即使参数列表为空)是明确表示它是模板实例化,而不是实际类型.
因此,我正在将我原来的问题转向类模板和函数模板案例之间的差异:为什么在第二个片段中允许调用无括号,与第一个片段中A的实例化相反?为什么不允许b?
template<int i = 1>
struct A {
operator int() { return i; }
};
template<int i = 2>
using B = A<i>;
// using C = A; <-- error: missing template arguments after 'A'
using C = A<>;
int main() {
A a; // Edit: Actually should require brackets: A<> a;
// B b; <-- error: missing template arguments before 'b'
B<> b;
C c;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用函数模板而不是类模板构建类似的场景,并且在最后一种情况(C)中存在细微差别:如果在定义中指定了返回类型,则不需要参数列表a.我想我理解为什么,但我会欢迎一些见解.否则,两种情况都类似于类模板.
template<int …Run Code Online (Sandbox Code Playgroud) 我真的不明白下面的代码发生了什么.为什么是t3零?
uint64_t t1 = MAXDWORD; // t1 contains 4294967295 - CORRECT
uint64_t t2 = t1 + 1; // t2 contains 4294967296 - CORRECT
uint64_t t3 = MAXDWORD + 1; // t3 contains 0 (zero) - HUH??
Run Code Online (Sandbox Code Playgroud) 如何在 Brainfuck 中使用循环打印从 1 到 10 的数字?甚至有可能吗?
我正在寻找解决此问题的方法。
我知道这k--等于k = k - 1.我想知道k = k--我认为可能导致无限循环的事情.但事实上,我在Visual Studio 2017中编译此代码并且输出与之k = k--相同k = k - 1.
那有什么意义k = k--呢?
我正在四处寻找要做的事情,我有了在 Windows 中的特定地址分配内存的想法。
所以我在 stackoverflow 上阅读了一些问题,但没有一个真正提供一个有效的例子,所以我不得不想出我自己的,因为我真的很想尝试:
#include <Windows.h>
#include <iostream>
struct Variable {
int var;
};
#define ACCESS() ((Variable*)0x50000000)
int main()
{
DWORD ptr;
VirtualAlloc((void*)0x50000000,sizeof(Variable),MEM_COMMIT | MEM_RESERVE | MEM_PHYSICAL,PAGE_READWRITE);
VirtualProtect((void*)0x50000000,sizeof(Variable),PAGE_READWRITE,&ptr);
ACCESS()->var = 5;
while(!GetAsyncKeyState('Q')){}
}
Run Code Online (Sandbox Code Playgroud)
但这总是会导致访问冲突..
在特定地址分配数据的正确方法是什么?因为,这种方式以某种方式行不通......同时与“为什么不呢?”混淆。
编辑:
第一个答案后的第二个代码也不起作用:
#include <Windows.h>
#include <iostream>
struct Variable {
int var;
};
#define ACCESS() ((Variable*)0x50000000)
int main()
{
std::cout << VirtualAlloc((void*)0x50000000,sizeof(Variable),MEM_COMMIT,PAGE_READWRITE) << std::endl;
std::cout << GetLastError() << std::endl;
ACCESS()->var = 6;
std::cout << ACCESS() << std::endl;
while(!GetAsyncKeyState('Q')){}
}
Run Code Online (Sandbox Code Playgroud)
返回值为 0,表示失败,GetLastError() 等于 487,
和 …
我的问题是关于 C 中的动态内存分配。我被要求动态分配一个nlong数组,并返回指向该数组第一个元素的指针。我有一些代码来测试这个输出,但内存分配失败。
long* make_long_array(long n)
{
int i;
int *a;
a = (int*)malloc(sizeof(int)*n);
if (a == NULL) {
printf("ERROR: Out of memory\n");
return 1;
}
for (i = 0; i < n; *(a + i++) = 0);
return *a;
}
Run Code Online (Sandbox Code Playgroud)
我在两行上收到一个错误说
'错误:返回使指针来自整数而不进行强制转换'
这发生在线路上
return 1;
Run Code Online (Sandbox Code Playgroud)
和
return *a;
Run Code Online (Sandbox Code Playgroud)
我不完全确定如何解决这个问题。我认为错误return 1;在于我在寻找指针时试图返回一个整数?但我不确定如何修复它以返回指针。任何帮助将非常感激。
问题1:我的朋友问我一个问题,我想证明我的理解是正确的。如果我声明一个特定大小的数组,但不指定每个元素的值,则将其打印出来,得到全0。但这是使它们正确为0的编译器吗?我只需要声明一个X大小的数组,就可以分配一个正确大小的连续块吗?我不应该指望它们始终为零,而应自己手动对其进行初始化。
问题2:我在留言板上看到,较新的c ++允许您声明具有用户定义大小的数组。这对我来说没有意义,我一直认为需要在编译时就知道它。怎么了 这是语法糖吗?到底发生了什么事?
#include <iostream>
using namespace std;
void question2()
{
int userInput;
cin >> userInput;
int anotherArray[userInput];
}
int main()
{
int array[5];
for (int i = 0; i < 5; i++) {
cout << array[i] << endl;
}
cout << endl;
question2();
}
Run Code Online (Sandbox Code Playgroud)
我希望从main发出混乱的输出,并显示类似“ userInput not const”之类的错误,但是我得到0 0 0 0 0和用户输入,并且进程在2.857秒后退出,返回值为0
我已经研究了几种解决方案来解决我的问题,但似乎没有什么对我有用。我希望我的输出能够对齐此代码上的所有姓名、所选号码和中奖金额。目前它输出:
3454 Atkins, Joe 7 6 5 4 3 2 1 7 $20.00
4321 Barber, John 11 22 7 8 45 12 10 1 $0.00
8976 Dollar, Kim 44 33 22 11 10 9 4 1 $0.00
Run Code Online (Sandbox Code Playgroud)
我相信我需要将名称右对齐才能解决此问题,但我尝试过的任何方法都不起作用。这是我的代码:
cout << student_info[i].id_num;
cout << setw(10) << student_info[i].student_name << setw(10);
for(int j = 0; j < LOTTERYNUMBERS; j++)
cout << student_info[i].lotteryNumbers[j] << " ";
cout << setw(10) << student_info[i].lotteryMatches << setw(10) << setprecision(2)
<< fixed << showpoint << "$" << student_info[i].prizeMoney << …Run Code Online (Sandbox Code Playgroud)