int main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
........
好的,这是来自他们网站的c++手册中的指针部分。我迷路了。
如果p是对数组的引用,那么第4行的增量是多少?或者它甚至指向数组numbers?
什么时候这会比仅仅重新分配数组中的值更好呢?
情况就是这样。
class Class1{
public:
Class1(int N){
p = new int [N];
}
~Class1(){
delete []p;
}
...
private:
int *p;
};
class Class2(){
public:
Class2(){
int N = somthing;
A = Class1(N);
...
}
...
private:
Class1 A;
}
基本上,Clas
我有一个网络,有一个巨大的组件和一些有限的组件。我需要计算有限分量(不包括巨型分量)的平均度数。为此,我尝试从所有组件的列表中删除巨型组件,并创建一个控制组件的子图
components = nx.connected_components(G) #list of all the components
GC = max(components, key=len ) #giant component
finite_cluster= components.remove(max(components, key=len )) #remove the GC from the component
int delta[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
random_shuffle(&delta[0],&delta[4]);
编译错误:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h: In function 'void std::swap(_Tp&, _Tp&) [with _Tp = int [2]]':
/usr/lib/gcc/x86_64-redhat-linux/
这就是我正在做的项目。任务是将2个矩阵相乘。矩阵A中的列数与矩阵B中的行数相同。我允许用户选择插入两个矩阵包含的内容。这是我的代码:
#include <stdio.h>
void multiplyMat(int n,int p,int c,int m,int d,int q,int k,int **multiply,
int **first,int **second);
int main()
{
int m, n, p, q, c, d, k;
int first[10][10], second[10][10], multiply[10][10];
printf(
谁能评论一下这里的问题所在?
编译器错误说:错误:不兼容的类型:‘数组’和‘名称’错误行: Student.s_name:=n;
代码如下。
program datafiles;
Uses WinCRT;
Type
studentRecord = Record
s_name: packed array[0..30] of char;
end;
name = packed array[0..30] of char;
Var
Student: StudentRecord;
n: name;
Begin
readln(n);
以下声明通过编译检查:
int arr[3];
vector<int[3]> vec; // ok !!
当尝试实际使用vec时,
vec.push_back(arr);
它会导致许多错误,如:
/usr/include/c++/4.6/ext/new_allocator.h:108:9: error: ISO C++ forbids initialization in array new [-fpermissive]
/usr/include/c++/4.6/bits/vector.tcc:314:4: error: invalid array assignment
/usr
我的Visual 10中有以下代码(简化示例):
int myfunc(void)
{
array <int^>^ temparr=gcnew array<int^>(11);
for (int i=0; i<11; i++)
{
temparr[i]=0;
}
temparr[2]=1; //one of the elements is 1
for (int i=0; i<11; i++)
{
if (!temparr[i]) return 0;
如果我有2维it队列,我希望能够像这样内联声明它:
int my_queue[$][$] = {{1, 2}, {3, 4}};
我也看到了
typedef int int_queue[$];
int_queue my_queue[$] = {{1, 2}, {3, 4}};
相反,当我编译时,VCS向我提供了一个不兼容的复杂类型错误:
Type of source expression is incompatible with type of target expression.
Mismatching types cannot be used in assignments, in
这应该很简单,已经花了几个小时了,但我仍然不能弄明白。
我只需要一个指向合并的2D数组的指针。
int a1[2][3] = {{1,2,3}, {4,5,6}};
int a2[4][3] = {{7,8,9}, {6,9,6}, {1,2,3}, {4,5,6}};
// This is fine
int (*p)[3] = a1;
// compilation error - incompatible types int[3] from type int (*)[3]
*(p + 2) = a2
我理解这个问题,但无法解决这个问题。
请有人告诉我这样的结构在C++中是否有效(即不是UB)。正因为如此,我有一些片段缺陷,我花了几天的时间试图弄清楚那里到底发生了什么。
// Synthetic example
int main(int argc, char** argv)
{
int array[2] = {99, 99};
/*
The point is here. Is it legal? Does it have defined behaviour?
Will it increment first and than access element or vise versa?
我是C++的初学者,我正在尝试为网格矩形创建一个Rect类,该类是使用两个向量创建的:start点(左上角)和dimensions。我想为Rect创建一个名为get_points()的方法,它返回矩形内所有网格点的向量。
以下是我到目前为止写的内容:
#include <cstdlib> //I will be using random numbers later
#include <vector>
class Rect {
private:
int* start, dimensions;
int area;
publ
我正在将一些C++代码翻译成ObjectPascal (第一次),而C++对我来说仍然是相当新的。
C++头文件如下所示:
class RenumFd {
public:
RenumFd(int length);
~RenumFd();
void CompFd(double *buff);
//...other public functions cut for space
private:
void rmfd(int n, int isgn, double *a, int *ap, double *kw);
//...other private funct
各位,我是c++的新手,想问几个关于在C++中分配内存的问题。抱歉,如果这看起来像是一个基本的问题,但下面我有一个使用malloc分配的内存地址。Malloc使用整数大小的倍数,这将返回一个空指针,但随后会转换为整数指针。如果在技术上不是指向整数大小的内存区域,而是整数大小内存的倍数,为什么允许对整数进行赋值? #include <stdio.h>
#include <stdlib.h>
int main() {
int n = 4, i, *p, s = 0;
p = (int*) malloc(n * sizeof(int));
if(p ==
我是C++的新手,目前正在努力学习WHILE循环。但是有一个问题我对我的代码不太理解,它给了我一个与我预期不同的结果。这就是:
int i = 1;
double ans = 1.00;
while (ans > 0.1) {
ans = 1 / i;
i++;
}
cout << "ans: " << ans;
我期望有: ans : 0.1,但一直给我: ans :0。你能告诉我我做错了什么吗?谢谢你的帮助。
我需要编写一个C++\CLI包装器。我的主要目标是处理在我的.NET代码中提供的一些像素数据。该算法用C++语言编写。
我有dll和C++代码的头,为了在C#中使用API,我需要编写一个包装器。
C++头
int Func1(unsigned short const* const* inputImage, int const* size, double const* res) ;
我不知道如何包装这种方法。给定的inputImage实际上是指向多个级联图像像素数据的指针。
我的包装.h
int Func1Wrapper(unsigned short const* const* inputIm
以下是我编写的Calc[] calculators = new Calc[10]; calculators[0].AddToSum(10);代码(相应的类和方法也已编写完成)。但是我得到了"Object reference not set to a instance of an object“exception.Then,通过一些研究,我通过如下操作消除了这个异常。
for (int i = 0; i < 10; i++)
{
calculators[i] = new Calc();
}
谁能解释一下为什么我们需要重新分
您好,我收到错误C2088:'+=':非法的联合错误在visual studio上...同样的代码在gcc上也能正常工作。你能告诉我在c++中用visual studio解决这个问题的解决方案吗?
typedef union {
float sm[8];
} Su;
typedef union {
Su v;
float bm[8];
} Bu;
int main() {
Bu A1, A2;
A2.v.sm[0] = 12.5;
// .... some assignments here
A1.v += A