小编roc*_*cko的帖子

一个地址如何存储多个值?

问题在标题中给出:我不知道为什么会发生这种情况.有人能告诉我这些技巧是如何运作的.

这是我的代码:

#include<stdio.h>
int main(){
    int a = 320;
    char *ptr;
    printf("%p\n",&a);
    ptr =( char *)&a;
    printf("%p\n",ptr);
    printf("%d\n",a);
    printf("%d\n",*ptr);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

0x7fffc068708c 
0x7fffc068708c
320
64
Run Code Online (Sandbox Code Playgroud)

c pointers

3
推荐指数
2
解决办法
128
查看次数

为什么C++ new没有返回指向它创建的对象的指针?

我理解下面的代码,根据我的知识,当我使用new声明一个对象时,它构造一个特定类型的对象并返回指向该对象的指针.但是当我使用new创建一个student对象时,它不会返回指向该对象的指针.此外,当我打电话给新学生时(s1), " 学生(学生*) "被称为" 学生(学生) ",而不是给出错误,例如没有从学生到学生的类型转换*

#include <bits/stdc++.h>

using namespace std;

class student {
    public:
        int cool;

        student(student* s){ // this constructor is taking pointer to s whereas i am calling it by value below
            this->cool=s->cool;
        }
        student(){
            this->cool=1;
        }
};
void blah(student s){
    printf("cool:%d\n",s.cool);
}

int main(){

    student s1=new student(); // here new is not returning student*
    s1.cool=2;
    student s2=new student(s1); // here we are passing s1 which of type student but constructor (student*) is called why ??? …
Run Code Online (Sandbox Code Playgroud)

c++ oop

3
推荐指数
1
解决办法
112
查看次数

广度优先搜索O(log n)

是否可以在带有循环和负边的无向图中使用BFS(使用遍历的最小顶点)在O(log n)时间内从源中找到目的地?

例如:给出一个简单的连通图G,其中包含N个顶点和N个边(一个简单的图是一个没有循环且在任意两个不同顶点之间不超过一条边的无向图).很明显,图G只包含一个周期,你可以假设这个周期的长度是奇数(这个周期中有奇数个顶点).顶点从1到N编号.每个边被分配相应的整数权重.您的任务是激发两种类型的查询:更新由fuv表示的查询:更改最短路径上边缘的所有权重的符号(稍后可以看到此问题中最短路径的定义)从顶点u到顶点v.查找由?表示的查询?uv:在从顶点u到顶点v的最短路径上,找到(可能为空)一组连续边,使得权重之和最大.换句话说,让我们将从u到v的最短路径定义为a_1,a_2,...,a_k(其中a_1 = u和a_k = v).你必须找到a_i和a_j使得i <= j并且路径a_i,a_(i + 1),...,a_j的边缘的权重之和尽可能大.你只需要找到这笔钱.两个顶点u和v之间的最短路径是将它们与最小数量的顶点连接起来的路径.在这个问题中,显然在G的任何一对顶点之间只有一条最短路径.

c c++ algorithm graph

2
推荐指数
1
解决办法
824
查看次数

在 C++ 中,double 能在 8 个字节中存储 1024 位吗?

我不明白这里发生了什么事。

我发现以下数据类型的大小(以字节为单位):

char:1
int:4
float:4
double:8
long long int:8

Now long long int max size is 9223372036854775807
whereas double max size is 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
Run Code Online (Sandbox Code Playgroud)

什么!!!

double 先生如何在短短 8 个字节中存储如此大的值???

c++ numeric-limits

-2
推荐指数
1
解决办法
1613
查看次数

标签 统计

c++ ×3

c ×2

algorithm ×1

graph ×1

numeric-limits ×1

oop ×1

pointers ×1