小编Sha*_*our的帖子

误解了虚拟功能在矢量中的作用

我在c ++上有以下代码:

#include <iostream>;
#include <vector>;

class A
{
public:
    A(int n = 0) : m_n(n) { }

public:
    virtual int value() const { return m_n; }
    virtual ~A() { }

protected:
    int m_n;
};

class B
    : public A
{
public:
    B(int n = 0) : A(n) { }

public:
    virtual int value() const { return m_n + 1; }
};

int main()
{
    const A a(1);
    const B b(3);
    const A *x[2] = { &a, &b };
    typedef …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance vector

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

cin一个非常大的int数组,段错误11

这是我的代码:

int main(){
    int n = 0;
    std::cin>>n;
    int lh[n][2];

    for(int i = 0; i < n; i++) {
        std::cin>>lh[i][0]>>lh[i][1];
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么,如果n非常小,它运行正常,但是当n更大,比如10,000,000时,就会发现segmentfault 11错误.

这里发生了什么 ?

c++

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

从函数返回指针(变量在函数中声明)

int* sum(int *mypt,int len){
int i;
int mysum = 0; 
for(i=0;i<len;i++)
  mysum += mysum;   
  return &mysum; // I cannot do this since the function frame goes away after the call.
}

int main()
{

  int *pt = (int*) malloc(3*sizeof(int));   
  pt[0] = 0;
  pt[1] = 1;
  pt[2] = 2;

  int *s = sum(pt,3);
  printf("%d\n",*s);    
  return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我想返回指针mysum.我不能这样做,int static sum = 0因为它是固定的.我无法使用,int const sum = 0因为它给出了一个错误""readonly"".这个问题有方法解决吗 ?

c pointers function return-type

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

如何终止从cin读取?

我已经尝试了这里列出的一堆方法,但没有一个起作用。它总是在等待更多的输入。

我已经尝试while(std::getline(std::cin, line))过以下方法,似乎没有任何效果:

#include <iostream>
#include <sstream>

using namespace std;

int main(){
  long length = 1UL<<32;
  int array[length];
  // memset(array, 0, (length-1) * sizeof(int));

  for(int i = 0; i < length; i++)
    array[i] = 0;
  string line;
  int num;
  while(!cin.eof()){
    getline(cin,line);
    stringstream ss(line);
    ss >>num;
    array[num]++;
  }
  for(int i = 0; i < length; i++)
      if(array[i]){
          cout << i << ": ";
          cout << array[i] << endl;
      }
}
Run Code Online (Sandbox Code Playgroud)

c++

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

如果两个var具有相同的值,为什么它将显示不相等

这是我的代码,任何人都可以告诉我为什么编译器打印"两个不相等"的消息,当我把相同的值放在变量ab.

(注意:我不想比较字符串,我只需要技术原因).

#include<stdio.h>
main()
{
    char a[3]="abc",b[3]="abc";
    if(a==b)
        printf("both are equal");
    else
        printf("both are not equal");
}
Run Code Online (Sandbox Code Playgroud)

c arrays string

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

虽然永远不会以随机数结尾

我试图得到一个随机数,并且如果它超过另一个数字则具有while结尾:

int main() // random
{
    int x= 50;
    int i;

    while (i>x){
        srand(time(0));
        int i = rand() %100;
        printf("laenge %d", i);
    }
}
Run Code Online (Sandbox Code Playgroud)

同时开始很好并产生不同的数字(如1.000倍11,1.000倍75 ......)但它永远不会结束.

我有2个问题,为什么不结束?为什么它会在控制台中获得相同随机数的1.000倍,然后是下一个随机数的1.000倍?

如果我添加:

int main() // random
{
    int x= 50;
    int i;

    while (i>x){
        srand(time(0));
        int i = rand() %100;
        printf("laenge %d", i);
        sleep(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

睡眠(1); 整个代码不再起作用了.

c random while-loop

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

如何理解和修复此代码中的Segmentation错误?

我是C++ Primer第5版的新手,它是:

练习6.33:编写递归函数以打印向量的内容.

我的代码,如下所示,编译时没有错误也没有警告.vector_print()在将第一个项目添加到矢量中之后,该函数通常会打印矢量.但是在添加第二个项目之后,程序被操作系统终止.

我试过调试.cout << *it <<" ";正在执行语句时生成一个分段错误.它变成了:

劣势停止是因为它收到了来自操作系统的信号.

信号名称:SIGSEGV信号含义:分段故障

我对这个错误做了一点介绍,但仍然没有得到它.谁能告诉我如何理解它并解决这个问题?:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

template<class T>
void  vector_print(const vector<T> &_v);

int main()
{
    string s;
    vector<string> v;

    cout<<"Please Enter:\n";
    while(cin>>s)
    {
        v.push_back(s);
        cout <<"the vector is:  --";
        vector_print(v);
    }
}

template<class T>
void vector_print(const vector<T> &_v)
{
    static typename vector<T>::const_iterator it = _v.begin();
    cout << *it <<" ";

    ++it;
    if(it != _v.end())
    {
        vector_print(_v);
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ static vector segmentation-fault

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

循环后为什么有分号while();

我正在阅读我朋友的代码我看到了:

#include <stdio.h>
#include <conio.h>

void main()
{
    char string1[125], string2 [10];
    int i, j;
    printf("\nstring 1: ");
    gets(string1);
    printf("\nNstring2 : ");
    gets(string2);
    i = 0;
    while (string1[i] != 0)
    {
        j = 0;
        while (string1[i++] == string2[j++] &&string1[i-1] != 0 && string2[j-1] != 0)
            ;//i dont know what it mean and why we can put ;after while loop
        if (string1[i-1] != 0 && string2[j-1] == 0)
        printf("\nfound at position %d", i-j);
    }
    getch();
}
Run Code Online (Sandbox Code Playgroud)

为什么我们可以放在;while循环后,任何人都可以帮忙?

c

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

警告:左移计数> =类型的宽度[默认启用]

我有以下代码:

long int compute_data_length(unsigned char* buf, int data_offset) {

long int len;

if(!buf)
    return -1;

switch(data_offset) {
    case 2: {
                len = (buf[2] << 8) | buf[3];
            }
            break;
    case 8: {
                len = (buf[2] << 56) |
                      (buf[3] << 48) |
                      (buf[4] << 40) |
                      (buf[5] << 32) |
                      (buf[6] << 24) |
                      (buf[7] << 16) |
                      (buf[8] <<  8) |
                      (buf[9]      );
            }
            break;
    default: len = -1; break;
}
return len;
}
Run Code Online (Sandbox Code Playgroud)

编译时,我收到以下警告:

math_fun.c:240:21:警告:左移计数> =类型的宽度[默认启用] len =(buf …

c int bit-shift undefined-behavior unsigned-char

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

如何在printf占位符中使用等于表达式?

我有以下代码片段:

main( )
{
int k = 35 ;
printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
}
Run Code Online (Sandbox Code Playgroud)

产生以下输出

0 50 0
Run Code Online (Sandbox Code Playgroud)

我不知道我理解的第一个值怎么printf0.当值k与之比较时35,理想情况下它应该返回(并因此打印)1,但它如何打印为零?产生的其他两个值 - 50并且0都是正确的,因为在第二个值中,k的值被视为50,而对于第三个值,k的值(即35)被比较40.因为35 < 40,所以它打印0.

任何帮助将不胜感激,谢谢.

**更新**

在研究了关于这个主题的更多内容之后undefined behavior,我在一本关于C的书中看到了这一点,最后给出了源代码.

调用约定 调用约定表示在遇到函数调用时参数被传递给函数的顺序.这里有两种可能性:

  1. 参数可能从左向右传递.
  2. 参数可能从右到左传递.

C语言遵循第二顺序.

考虑以下函数调用:

fun (a, b, c, d ) ;
Run Code Online (Sandbox Code Playgroud)

在这个调用中,参数是从左向右还是从右向左传递并不重要.但是,在某些函数调用中,传递参数的顺序成为一个重要的考虑因素.例如:

int a = 1 ;
printf …
Run Code Online (Sandbox Code Playgroud)

c printf equality undefined-behavior assignment-operator

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