小编san*_*n45的帖子

对链表更好的结构或类?

对于链表实现更好

使用结构

#include <iostream>

using namespace std;

struct Node {
    int data;
    Node* next;
};
Run Code Online (Sandbox Code Playgroud)

使用课程

class ListNodeClass
   {
   private:
      ItemType Info;
      ListNodeClass * Next;
   public:

      ListNodeClass(const ItemType & Item, ListNodeClass * NextPtr = NULL):
         Info(Item), Next(NextPtr)
            {
            };
      void GetInfo(ItemType & TheInfo) const;
   friend class ListClass;   
   };

typedef ListNodeClass * ListNodePtr;
Run Code Online (Sandbox Code Playgroud)

或者他们是否有更好的方法在C++中进行链表?

c++

8
推荐指数
1
解决办法
3961
查看次数

类型的非const引用的无效初始化错误

下面是从字符串中查找和替换子字符串的代码.但是我无法将参数传递给函数.

错误信息 :

从'const char*'类型的右值开始,无效初始化'std :: string&{aka std :: basic_string&}'类型的非const引用

请帮忙解释一下

#include <iostream>
#include <string>
using namespace std;

void replaceAll( string &s, const string &search, const string &replace ) {
    for( size_t pos = 0; ; pos += replace.length() ) {
        pos = s.find( search, pos );
        if( pos == string::npos ) break;
        s.erase( pos, search.length() );
        s.insert( pos, replace );
    }
}
int main() {

    replaceAll("hellounny","n","k");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
9203
查看次数

在递增/递减运算符的情况下AND的短路

在下面的代码中:

#include <stdio.h>

int main()
{
     int a = 1;
     int b = 1;
     int c = a || --b;
     int d = a-- && --b;
     printf("a = %d, b = %d, c = %d, d = %d", a, b, c, d);
     return 0;
}
Run Code Online (Sandbox Code Playgroud)

我期待输出是:

A = 0,B = 1,C = 1,d = 0

因为由于下面一行中的短路,即a--返回0,所以另一部分不会被执行正确?

int d = a-- && --b;
Run Code Online (Sandbox Code Playgroud)

输出是:

a = 0,b = 0,c = 1,d = 0

有人可以解释一下吗?

c short-circuiting logical-operators

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

preincremented variable(++ i)++的post incement错误?

在下面的代码段中,我希望答案为5,但它显示编译时错误:

#include <stdio.h>
int main()
{
    int i = 4;
    printf("%d", (++i)++);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

是什么原因?.这里++i返回一个l值,所以我们可以增加它吗?

c

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

代码中的运行时错误

在以下程序中旋转一个字符串运行时发生错误.请帮助

代码中没有编译错误

#include <iostream>
#include <cstring>

using namespace std;

void reverseString(char* str, int start, int end)
{
    int front = start;
    int back = end;
    while (front < back)
    {
        /* swap two variables without
        using a temporary one.*/
        str[front] ^= str[back];
        str[back]  ^= str[front];
        str[front] ^= str[back];
        ++front;
        --back;
    }
    return;
}
Run Code Online (Sandbox Code Playgroud)

这部分用于旋转弦

void rotateString(char* str, int k)
{
    if (!str || !*str)
        return;
    int len = strlen(str);
    /*Rotating a string by it's length is string itself.*/
    k %= …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
109
查看次数

标签 统计

c++ ×3

c ×2

logical-operators ×1

short-circuiting ×1