我一直在玩,auto我注意到在大多数情况下你可以用一个变量定义替换auto,然后分配类型.
在下面的代码w和x都相等(默认情况下初始化int,还是让我们无法进入潜在副本).有没有办法声明z它具有相同的类型y?
int w{};
auto x = int{};
int y[5];
auto z = int[5];
Run Code Online (Sandbox Code Playgroud) 这个问题是关于采用静态已知大小的数组的函数.
以下面的最小程序为例:
#include <iostream>
template<size_t N>
void arrfun_a(int a[N])
{
for(size_t i = 0; i < N; ++i)
std::cout << a[i]++ << " ";
}
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
arrfun_a<5>(a);
std::cout << std::endl;
arrfun_a<5>(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行时,打印预期结果:
2 3 4 5 6
3 4 5 6 7
Run Code Online (Sandbox Code Playgroud)
但是,当我试图让我的编译器(VS 2010)推断出5它时could not deduce template argument for 'int [n]' from 'int [5]'.
一些研究导致arrfun_b模板参数推导工作的更新:
template<size_t n>
void arrfun_b(int …Run Code Online (Sandbox Code Playgroud) 可能重复:
c ++*vs&in函数声明
我知道这对你们许多人来说似乎是一个非常基本的问题,但我真的有一个不可能的时间找到一个好的,彻底的解释,尽管我最好的谷歌搜索.我确定答案就在那里,所以我的搜索条件一定很糟糕.
在C++中,各种符号及其组合用于标记参数(以及这些参数的参数).究竟是什么意思呢?
例:void func(int *var)和之间有什么区别void func(int **var)?怎么样int &var?
同样的问题代表返回类型以及参数.int& func(int var)与int* func(int var)?相比,意味着什么?在论证中,有何y = func(*x)不同y = func(&x)?
如果你能指出我正确的方向,我非常乐意阅读关于这个主题的大量文章.另外,我对一般编程概念非常熟悉:OO,泛型/模板等,而不是C/C++中使用的符号.
编辑: 似乎我可能给人的印象是我不知道指针是什么.我想知道那是怎么回事:)
所以为了澄清:我完全理解指针是如何工作的.我没有抓住,奇怪地无法找到答案的意思是,例如'void func(int&var)'.在一个赋值语句的情况下,"&"运算符将是对右侧,如在"诠释*X =&Y;",但在上述中,"&"操作者实际上是在左手侧.换句话说,它是在l值上运行,而不是在r值上运行.这显然不具有相同的含义.
我希望我现在更有意义吗?
我的方法有问题.我希望它接受一个字符串数组作为它的第一个参数而不是矢量字符串.但是,当我尝试使用一个字符串数组并在main函数中创建一个字符串时,我会遇到各种错误.我不知道是否应该为我的参数或字符串使用指向字符串数组的指针.有帮助吗?
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <sstream>
#include<iostream>
using namespace std;
class UserName
{
public:
string newMember(string* exist, string newname) {
bool found = false;
bool match = false;
stringstream ss;
string result;
string othername;
for(int i = 0; i < exist.size(); i++){
if(exist[i] == newname){
found = true;
break;
}
}
if(found){
for(int x = 1; ; x++){
match = false;
ss.str("");
ss << newname << x; …Run Code Online (Sandbox Code Playgroud) 我该如何从函数返回一个数组?我的代码是
float ClassArray::arr_sub(float a[100][100], float b[100][100]) {
int i,j;
for(i = 1; i < 10; i++) {
for(j = 1; j < 10; j++){
f[i][j]=b[i][j]-a[i][j];
}
}
return f;
}
Run Code Online (Sandbox Code Playgroud)
并且应该将从此函数返回的f分配给在其他类中声明的另一个数组g.
float g[100][100];
g= cm.arr_sub(T,W);
Run Code Online (Sandbox Code Playgroud)
但是在建造课程时,它说incompatible type assignment of float to float[100][100].
我想在我的类的头文件中声明一个指向整数数组的指针.我有
private:
int *theName;
Run Code Online (Sandbox Code Playgroud)
然后在构造函数中
theName = new int[10];
Run Code Online (Sandbox Code Playgroud)
这会有用吗?还有另一种方法可以做到这一点,所以你有一个指向数组的指针吗?
我以为int*theName创建了一个指向整数而不是整数数组的指针,如果这创建了一个指向整数数组的指针,那么如何创建一个只指向整数的指针?
我也可以在析构函数中调用
delete theName; theName = NULL;
Run Code Online (Sandbox Code Playgroud)
是否会删除name整数数组对象,然后将name指向内存中的null?
谢谢
我试图在我的类构造函数中初始化指向struct数组的指针,但它根本不起作用...
class Particles {
private:
struct Particle {
double x, y, z, vx, vy, vz;
};
Particle * parts[];
public:
Particles (int count)
{
parts = new Particle [count]; // < here is problem
}
};
Run Code Online (Sandbox Code Playgroud)