小编Che*_*Alf的帖子

为什么这个程序总是在最后显示U.

我为加密词创建了以下C代码.(caesar cipher)当我运行它时,它总是打印U.如果你运行它,你会看到它.

#include<stdio.h>

int main(void){
    int x;
    char en[100];

    fgets(en,100,stdin);
    for(x=0;x<100;x++){
        if(en[x]=='\0'){
            break;
        }
        en[x]=((en[x]-71-3)%26)+97;
    }
    printf("%s\n",en);
}
Run Code Online (Sandbox Code Playgroud)

c

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

使用Variadic模板的模板元编程:编译器错误

我是第一次尝试变量模板元编程,并且始终遇到我无法追踪的编译器错误.

我正在关注此页面上的"元组"示例(虽然我正在调用我的对象ItemSet)

ItemSet部分编译得很好:

template<typename...Ts> class ItemSet { };

template<typename item_T, typename ...Ts>
class ItemSet<item_T, Ts...> : public ItemSet<Ts...>
{
public:
    ItemSet(item_T t, Ts... item_types) : ItemSet<Ts...>(item_types...), tail(t) { }

protected:
    item_T tail;
};





template <typename...M> struct type_holder;

template<typename T, typename ...M>
struct type_holder<0, ItemSet<T, M...>>
{                          // ERROR: M parameter pack expects a type template argument.
    typedef T type;
};

template <int k, typename T, typename ...M>
struct type_holder<k, ItemSet<T, M...>>
{
    typedef typename …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates

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

为什么将向量视为通过值传递,即使它是由const引用传递的?

我最近编写了一个非常简单的代码class,它负责查找a中的min和max值std::vector,即使我将集合作为a const reference传递给class'构造函数然后更改向量(即将元素推入其中)或者从外面移除一个),内部的向量class保持不变.

#include <vector>
#include <algorithm>

class MinMaxFinder
{
public:
    MinMaxFinder(const std::vector<int>& numbers) : numbers(numbers)
    {
    }
    const int Min() const
    {
        this->findMinAndMax();
        return this->min;
    }
    const int Max() const
    {
        this->findMinAndMax();
        return this->max;
    }

protected:
    std::vector<int> numbers;
    int min;
    int max;

    void findMinAndMax()
    {
        // std::minmax_element call to find min and max values
    }
};
Run Code Online (Sandbox Code Playgroud)

我认为传递引用的点是为了不复制大对象,但是我的代码现在的工作方式似乎是复制集合.

#include <vector>
#include <iostream>
#include "MinMaxFinder.h"

int main(int argc, char* argv[])
{
    std::vector<int> …
Run Code Online (Sandbox Code Playgroud)

c++ oop reference vector c++11

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

结构名称不影响变量名称

我注意到以下代码编译了最近的编译器:

int main()
{
    int x;
    struct x;
    x = 210;                  // ?
}
Run Code Online (Sandbox Code Playgroud)

我记得几年前它没有编译.

是否在C++ 11或C++ 14中更改了查找规则以使此代码"正常工作"(从而打破使用struct variable_name;以确保在以下代码中不使用该变量)?


更新:显然我记得错了.我已经验证了即使使用Visual C++ 2010也可以编译好的代码.但是,当用于参数时,struct名称位于内部作用域和阴影中,如下面的代码所示:

void foo( int x )
{
    struct x;
    x = 210;                  // ? Error
}

int main()
{
}
Run Code Online (Sandbox Code Playgroud)

因此,我选择了"解决方案",没有改变的答案; 规则总是这样.

c++ c++11 c++14

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

如何将基本智能指针转换为 C++ 中的派生指针?

代码 :

class Base {
    ...
};

class Derived : public Base {
    void OnlyOwnedByDerived{
        ...
    }
};
Run Code Online (Sandbox Code Playgroud)

问题是: 1. 如果我使用基类的智能指针来引用派生类,这样做的原因是我想获得只适合虚函数的动态绑定的好处。但是如果我想使用只属于派生类的函数,我该怎么做? static_cast不同类的智能指针之间给了我一个错误......

  1. 我能想到的最直接的方法是使用原始指针而不是智能指针......

c++ pointers

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

为什么在 c 中实现链表时使用动态内存分配(即 malloc())?

好吧,对于业余程序员来说,这个问题可能听起来很愚蠢。但严重的是,这让我感到困扰,欢迎对我的这个疑问做出严肃的回答。我刚刚开始学习我的第一门数据结构课程。而困扰我的是:

假设使用 C,

//Implementing a node

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

现在,在创建节点时,为什么我们在使用 malloc() 的地方使用动态内存分配技术。我们不能只创建一个“结构节点”类型的变量。即类似:

struct Node N1;
 //First node - actually second where !st Node is assumed to be Head.

struct Node *Head = &N1;
struct Node N2;
N2.(*Node) = &N1;
Run Code Online (Sandbox Code Playgroud)

好吧,我的代码的某些部分可能不正确,因为我只是一个初学者并且不精通 C。但是知道您可能已经理解我的基本意思。为什么我们不创建 Node 类型的 Node 类型的变量来分配内存 t 新节点为什么要进入动态内存分配的复杂性?

c struct memory-management data-structures

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

'this' 不能用在带有 this-&gt; 指针变量的常量表达式错误 (C++) 中

我收到此错误消息,我知道这是因为数组中的参数必须是常量,但我不确定如何安排我的 'this-> V; 保持不变。有什么帮助吗?我提供了大部分代码,以便指针变量的功能在整个程序中都很明显。

// A class that represents an undirected graph
class Graph
{
int V;    // number of vertices
list<int> *adj;    // A dynamic array of adjacency lists
int *in;
public:
// Constructor and destructor
Graph(int V);
~Graph() { delete[] adj; delete[] in; }

// function to add an edge to graph
void addEdge(int v, int w) { adj[v].push_back(w);  (in[w])++; }

// Method to check if this graph is Eulerian or not
bool isEulerianCycle();

// Method to check …
Run Code Online (Sandbox Code Playgroud)

c++ error-handling

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

澄清了c stdlib.h中的RAND_MAX和rand()

为什么以下c代码产生的实数仅在0到1之间(例如:0.840188,0.394383 ...等),double a,b当RAND_MAX的值出现时0.000000.不应该RAND_MAX设置rand()函数生成的数字的最大值?

    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {

    double a,b,c;
    for (int i=0;i<100;i++){
    a=(double)rand()/(double)RAND_MAX;
    b=(double)rand()/(double)RAND_MAX;
    c=a-b;
    printf("itteration : %d values a=%f,b=%f,c=%f, RAND_MAX=%f \n",i,a,b,c,RAND_MAX);
    }  
    return 0;
    }
Run Code Online (Sandbox Code Playgroud)

c gnu ansi ansi-c

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

为什么这段代码不能编译?

有了const,如评论所示,msvc 11和g ++ 4.7.0拒绝编译:

#include <memory>       // std::unique_ptr
#include <utility>      // std::move
using namespace std;

struct CommandLineArgs
{
    typedef unique_ptr<
        wchar_t const* const [],
        void(*)( wchar_t const* const* )
        > PointerArray;

    //PointerArray const  args;         // Oops
    PointerArray        args;
    int const           count;

    static wchar_t const* const* parsed(
        wchar_t const       commandLine[],
        int&                count
        )
    {
        return 0;
    }

    static void deallocate( wchar_t const* const* const p )
    {
    }

    CommandLineArgs(
        wchar_t const   commandLine[]   = L"",
        int             _               = 0
        ) …
Run Code Online (Sandbox Code Playgroud)

c++ constructor const move-semantics

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

指针,C++ ......代码无效

这是一个问题:编写具有五个参数的函数minMax的定义.前三个参数是整数.最后两个由函数设置为前三个参数的最大值和最小值.该函数不返回值.

该功能可以使用如下:

int a = 31, b = 5, c = 19, big, small; 
minMax(a, b, c, &big, &small); /* big is now 31; small is now 5 */ 
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

void minMax(int x, int y, int z, int* big, int* small)
{
  if (x < y && x < z)

    *small = x;

  else if (y < x && y < z)

    *small = y;

  else if (z < x && z < y)

    *small = z;

  if (x > …
Run Code Online (Sandbox Code Playgroud)

c++

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