我有以下问题是否有可能在c ++中使用以下规则创建头文件.让我们拿标题
#include "sum.h"
Run Code Online (Sandbox Code Playgroud)
在sum.hi中将声明函数或过程,然后在.cpp文件中使用它?
例如,假设一些k
是任意数字= 2 k=2
,那么C中有代码:
int wordcomp(char *p, char *q) {
int n = k;
for ( ; *p == *q ; p++, q++) {
if ( *p==0 && --n == 0 )
return 0;
}
return *p - *q;
}
Run Code Online (Sandbox Code Playgroud)
请解释一下这段代码的用途?还有什么*p-*q
意思呢?另外,它是如何在Java中实现的?
我们可以在结构中描述函数吗?例如,这段代码有效吗?
struct function {
int func() { return 5; }
};
Run Code Online (Sandbox Code Playgroud) 这是我的代码,从中无法理解错误在哪里
#include <iostream>
#include<iomanip>
using namespace std;
#define narray 8;// array size;
#define nbucket 5;// bucket size;
#define interval 10;// bucket range
struct node
{
int data;
struct node *next;
};
void BucketSort(int arr[]);
struct node *InsertionSort(struct Node *list);
void print(int arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(int value);
void BucketSort(int arr[])
{
int i,j;
struct node **buckets;
buckets = (struct node **)malloc(sizeof(struct node*) * nbucket);
for (i=0;i<nbucket;i++){
buckets[i]=NULL;
}
for (int i=0;i<narray;i++){
struct node *current;
int pos=getBucketIndex(arr[i]);
current=(struct node …
Run Code Online (Sandbox Code Playgroud) 我想说的是自学.
我们有两个整数.我想得到第三个元素,它等于两个整数之间的XOR,但是有约束.好的,让我举一个例子来说明一点.
int x is, let's say, is 10 `x = 10 //Binary 1010` and `int y = 9 //Binary 1001`
int t = x^y, where ^ is an operator that is defined as described below.
Run Code Online (Sandbox Code Playgroud)
但第一比特在x
应的第二位进行异或运算y
,并被存储为第一比特t
和第二比特x
进行异或运算与y中的第一位,并存储在吨等所述第二位.
结果应该是:
t = x^y = 1100
Run Code Online (Sandbox Code Playgroud)
我希望你能理解这个问题.如果没有,我会尽力澄清.
我已阅读以下教程,但我不太了解.何时使用longjmp以及为什么需要知道?
http://www.cplusplus.com/reference/clibrary/csetjmp/longjmp/
这段代码没有显示任何输出
#include <cstdlib>
#include <iostream>
using namespace std;
int partition(int a[], int left, int right) {
int i = left;
int j = right;
int temp;
int pivot = a[left];
while(i <= j) {
while(a[i] < pivot)
i++;
while(a[j] > pivot)
j--;
if(i>j) break;
if (i<j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
return i;
}
int randomized_partition(int a[], int left, int right){
int t = left + rand() % (right - left + …
Run Code Online (Sandbox Code Playgroud) 我试图实现Stack Overflow问题C++ Data Member Alignment和Array Packing.这是代码:
#include <stdio.h>
#include <stddef.h>
typedef struct {
unsigned char a;
unsigned char b;
unsigned char c;
}foo;
typedef struct{
unsigned short i;
unsigned char a;
unsigned char b;
unsigned char c;
} Bar;
typedef struct {foo f[5];} f_b;
typedef struct {Bar[5];} b_f;
#define ALIGNMENT_OF(t) offsetof( struct { char x; t test; }, test )
int main(void){
printf("Foo:: Size: %d; Alignment: %d\n", sizeof(foo), ALIGNMENT_OF(foo));
printf("Bar:: Size: %d; Alignment: %d\n", sizeof(Bar), ALIGNMENT_OF(Bar));
printf("F_B:: Size: …
Run Code Online (Sandbox Code Playgroud)