小编Bil*_*nch的帖子

C++运算符重载是否相等

我对C++很陌生,试图让这个简单的例子起作用,但由于某种原因,我得到了意想不到的结果.

码:

#include<iostream>

using namespace std;

struct node{
    string data;
    node *next;
    node *prev;

    node() {}

    bool operator==(const node &rhs) const {
        return data == rhs.data;
    }
};

int main(){
    bool loop=true;
    node* one = new node();
    one->data = "oddly specific";

    node* two = new node();
    two->data = "Something else";

    node* three = new node();
    three->data = "oddly specific";

    if (one == two)
        cout << "one == two";
    else
        cout << "one != two";
    if (one == three)
        cout << …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

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

如何在(C)中打印链表上的所有节点

我的链表只打印最后一个节点.我怎样才能解决我的问题;

我有一个函数将样本数据填充到链表中,然后打印所有节点.但我的代码只打印此图像:  代码输出是

我的功能是:

void FillSamples()
{
    db = (database *)malloc(sizeof(database));
    db->name = "College";

    tables=NULL;

    char na[5];
    int i = 0;
    for (i = 1; i <= 20; i++) {

        temptable = (table *)malloc(sizeof(table));

        itoa(i, na, 10);

        temptable->name = na;
        temptable->next = tables;

        tables = temptable;
    }

    // create links
    db->tables = tables;

    // print sample
    printf("database name = %s \n", db->name);
    temptable=tables;

    while (temptable)
    {
        printf("tables name = %s \n", temptable->name); 
        temptable=temptable->next;
    }
}
Run Code Online (Sandbox Code Playgroud)

c linked-list

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

数组的初始化

我正在练习的代码的一部分如下:

void outputBarChart()
{
    cout<<"\nOverall grade distribution: "<<endl;
    const int frequencySize = 11;
    int frequency[frequencySize] = {};

    for(int student = 0; student < student; ++student)
        for(int test = 0; test < tests; ++test)
            ++frequency[ (grades[student][test])/10 ];

    for(int counter = 0; counter < frequencySize; ++counter)
    {
        if (counter == 0)
            cout<<" 0-9: "<<endl;
        else if (counter == 10)
            cout<<" 100: "<<endl;
        else
        {
            cout<<counter * 10<<"-"<<(counter*10)+9<<": ";
            for (int stars = 0; stars < frequency[counter]; ++stars)
                cout<<"*";
            cout<<endl;
        }

        //cout<<endl;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ arrays

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

std :: pair在std :: map中使用

我的程序有问题.到达时我收到错误

cout << it->second << endl;
Run Code Online (Sandbox Code Playgroud)

我的节目:

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main() {
    map<pair<int, int>, int> kwadraty;
    long long ile;
    cin >> ile;
    int temp1, temp2;
    for(int i = 0; i < ile; i++)
    {
        cin >> temp1 >> temp2;
        kwadraty[pair<int, int>(temp1, temp2)]++;
    }

    for(map<pair<int, int>, int>::iterator it; it != kwadraty.end(); it++)
    {
        cout << it->second << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以看到问题出在哪里?

c++ c++11

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

不存在从“std::string”到“const char *”的合适的转换函数 c++

这是一个用于版本号目录的简单程序。它无法编译,出现此错误:

不存在从“std::string”到“const char *”的合适转换函数

#include <iostream>
#include <stdio.h>
#include <io.h>

using namespace std;

int main() {
    int max = 10, daily_patch = 0, monthly_patch = 0, yearly_patch;

    cout << " Enter the yearly_patch Number : ";
    cin >> yearly_patch;

    for (int i = yearly_patch; i <= yearly_patch; i++) {
        for (int j = 0; j < max; j++) {
            for (int k = 0; k < max; k++) {
                string str1 = to_string(i);
                string str2 = to_string(j);
                string str3 = …
Run Code Online (Sandbox Code Playgroud)

c++

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

交换指针数组中的元素

我在 C 中有以下数组(使用随机名称)

char * inputs[6] = {
  "Kangaroo my shoe", "Fly high dragonfly",
  "Philosophical Monkey", "Jumping Ape",
  "Fearful lemurs", "Tall Giraffes"
};
Run Code Online (Sandbox Code Playgroud)

目标是用下一个交换第一个元素。就像在做这个

char * inputs[6] = {
  "Fly high dragonfly", "Kangaroo my shoe",
  "Jumping Ape", "Philosophical Monkey",
  "Tall Giraffes", "Fearful lemurs"
};
Run Code Online (Sandbox Code Playgroud)

我已经尝试了以下以及更多。

ArrSwap(char *Arr[]){
  int i;
  for(i=0;i<6;i++){
    void*temp = Arr[i];
    Arr[i]=Arr[i+1];
    Arr[i+1] = temp;
  }
} 

Run Code Online (Sandbox Code Playgroud)

提前致谢!我花了几个小时试图弄清楚这一点,每次我这样做都会以混乱的顺序结束,或者没有交换第一个和最后一个预期的任何元素。

c arrays swap pointers

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

为什么它抱怨整数常量对其类型来说太大

我正在编写一个汉明权重计算器,但为什么数字 3 对于 uint32_t 来说太大了?

编写一个函数,它接受一个无符号整数并返回它所具有的“1”位的数量(也称为汉明权重)。

笔记:

请注意,在某些语言(例如 Java)中,没有无符号整数类型。在这种情况下,输入将以有符号整数类型给出。它不应该影响您的实现,因为整数的内部二进制表示形式是相同的,无论是有符号的还是无符号的。

在 Java 中,编译器使用 2 的补码表示法来表示有符号整数。因此,在示例 3 中,输入表示有符号整数。-3。

// package LeetCode Problem.Problem 2;
// Write a function that takes an unsigned integer and returns the number of '1'
// bits it has (also known as the Hamming weight).

#include <iostream>
using namespace std;

int hammingWeight(uint32_t n);

class BitShifting {
 public:
  uint32_t n;
  int hammingWeight(uint32_t n);
  void setn(uint32_t n);
};

void BitShifting::setn(uint32_t n) {
  n = n;
}

int BitShifting::hammingWeight(uint32_t n) {
  int …
Run Code Online (Sandbox Code Playgroud)

c++ bit-manipulation

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

这个三元表达怎么了?

我只是想在display: block;和之间切换display: none;.

($('#menu').css('display') = "block") ? $('#menu').css('display', 'none') : $('#menu').css('display', 'block');
Run Code Online (Sandbox Code Playgroud)

javascript jquery

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

请告诉我哪里出错了

#include <stdio.h>
main()
{
    char name1[15],name2[15],name3[15];
    int no;

    printf("Enter the serial number and name one\n");
    scanf("%d %15c',&no, name1");
    printf("%d %15s\n\n",no,name1);

    printf("Enter serial number and name two\n");
    scanf("%d %s",&no,name2);
    printf("%d %15s\n\n",no, name2);

    printf("Enter serial number and name three");
    scanf("%d %15s",&no,name3);
    printf("%d %15s\n\n",no,name3);
}
Run Code Online (Sandbox Code Playgroud)

嗨,我是编程的新手,我从C开始是出于某种原因.我上面输入的代码是我想要执行的程序.当我用Code :: Blocks执行它时,它运行直到输入序列号并命名为1然后如果我输入一个数字它就没有响应.然后我尝试使用Visual Studio 2013进行编译,它再次停止响应.

之后我尝试使用Visual Studio 2013调试器进行调试,它在temp.exe中的0x7575B790(msvcrt.dll)上说这是第一次机会异常:0xC0000005:访问冲突写入位置0x00000000.在按下继续之后,它在temp.exe中的0x7575B790(msvcrt.dll)处说这个未处理的异常:0xC0000005:访问冲突写入位置0x00000000. 我在Code :: Blocks之前编写了这个程序,它们都运行良好.请告诉我哪里出错了.另请说明使用%s.

谢谢

c debugging

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

将全局变量定义为c中的局部变量

我尝试过这样的事情:

int globalvar=10;

void print ()
{
    printf("%d \n",globalvar);
}

int main(){
    int globalvar=5;
    printf("%d \n",globalvar);
    print ();
    while (1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然后输出像这样

5    
10
Run Code Online (Sandbox Code Playgroud)

我已经得出结论,C在main中创建了一个"globalvar"的LOCAL实例,并用printf打印出的值10初始化.

我没关系,但我对一些新观点感到困惑:

  1. 在main之前初始化的globalvar在DS中具有相同内存位置的程序的整个范围,如果在函数中本地定义了一个具有相同名称的变量(ex:main),会发生什么情况, ?

  2. 第二个问题与链接器有关:链接器如何处理这两个相同名称的变量,以便它以执行的方式执行?

PS:在低调问题之后,我想说我真的搜索了这个问题并且我没有找到类似的问题,我认为提出这样的问题对其他人有用.我希望我不是误导

c

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