小编zw3*_*324的帖子

混乱std :: cout的奇怪输出

C++的std :: cout似乎是一件有趣的事情.我今天在C++编译器上尝试了以下程序:

cout<<"!"<<"@"<<endl;
cout<<"!"<<cout<<"@"<<endl;
cout<<"!"<<(cout<<"@")<<endl;
Run Code Online (Sandbox Code Playgroud)

产出很奇怪:

!@
!0x601068@
@!0x601068
Run Code Online (Sandbox Code Playgroud)

第一行是行人; 第二是可以理解的; 然而,第三行是我所不知道的.有人可以解释输出吗?谢谢大家提前!

子瑶薇

c++ cout std

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

如何使用BorderLayout/GroupLayout从JPanel中删除特定元素?

我需要删除ComponentCenterJPanel,但一些尝试之后没有占上风.

我试过这个方法:

使用BorderLayout从JPanel中删除CENTER元素

但答案的方法会产生编译时错误:

Type mismatch: cannot convert from LayoutManager to BorderLayout
Run Code Online (Sandbox Code Playgroud)

我是否错误地解释了答案?

另外,如果我只能从GroupLayout更新一个组件,我也很好奇.有人能告诉我怎么做吗?

编辑:@mre:这是代码:

BorderLayout layout = panel.getLayout();
panel.remove(layout.getLayoutComponent(BorderLayout.CENTER));
Run Code Online (Sandbox Code Playgroud)

这与链接基本相同.

谢谢你们!

java layout swing grouplayout border-layout

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

动态多维数组

我尝试了所有的东西,从我的理解,这段代码是正确的,但它仍然给我的分段错误.救命?

#include <stdio.h>
#include<malloc.h>

void da(int ***array, int row, int col){
    int i;
    *array=(int **)malloc(sizeof(int *)*row);
    for (i=0; i<row; i++)
        *array[i]=(int *)malloc(sizeof(int)*col);   
}

main(){
    int **array;
    int i,n,m;
    printf("Input number of rows: ");
    scanf("%d",&n);
    printf("Input number of columns: ");
    scanf("%d",&m);
    da(&array,n,m);
    for (i=0; i<n; i++)
        free(array[i]);
    free(array);
}
Run Code Online (Sandbox Code Playgroud)

c malloc multidimensional-array

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

如何对Vector进行排序并删除相同的值?

我有一个包含数据的向量,如:15,27,40,50,15,40

我想对它进行排序并删除相同的值,因此排序后的输出应为:15,27,40,50

我尝试了几种方法:

std::sort(vectProjHori.begin(),vectProjHori.end());
for (std::vector<int>::iterator it=vectProjHori.begin(); it!=vectProjHori.end(); ++it)
{
    if(it+1 != it)
    {
        std::cout << ' ' << *it;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,它无法在向量中删除相同的值.我真的希望有人愿意提供一种有效的方法.

任何帮助将受到高度赞赏.谢谢

c++ algorithm vector

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

我最喜欢的Segfault !! 为什么?

为什么会这样,导致段错?

#include<stdio.h>
#include<stdlib.h>


 struct node 
  {
      double d;
      int *array;
      char c;
  };



  void allocator(struct node *ptr)
  {
      int *tmp;
      tmp = (int*)realloc(ptr, 10);
      if(!tmp)
      {
        ptr->array=tmp;
        ptr->array[0] = 23;
      }
  }


  int
   main()
   {
      struct node *ptr = (struct node*)malloc(sizeof(struct node));
      ptr->c = 'y';
      allocator(ptr);

      printf(" %c\n", ptr->c);      
      printf(" %d\n", ptr->array[0]);
      return 0;
   }
Run Code Online (Sandbox Code Playgroud)

我得到的印象好像分配器函数中的realloc()分配内存,该内存也映射到main中malloc()分配的内存.

但这怎么可能发生?内存管理器(我猜这里的lib(stdlib))是否跟踪进程中的空闲和分配空间?

c memory memory-management

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

使用数组创建的对象,无法调用它的方法!! 为什么?

我创建了2个源代码文件,Shirt.javaShirtTestArray.java

我面临的问题是,每当我尝试访问displayShirtInformation()方法时,我都无法做到这一点..并获得错误类,接口或枚举预期

下面给出了两个源文件 -

Shirt.java--

public class Shirt {

  public int shirtID = 0; // Default ID for the shirt
  public String description = "-description required-"; // default

  // The color codes are R=Red, B=Blue, G=Green, U=Unset
  public char colorCode = 'U';
  public double price = 0.0; // Default price for all shirts
  public int quantityInStock = 0; // Default quantity for all shirts

  public Shirt() {
  }

  public Shirt(int ID, String d, char c, double …
Run Code Online (Sandbox Code Playgroud)

java

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

如何解决malloc()破坏指针的情况?

这是我刚才写的函数的简化版本:

int foobar(char * foo) {
  puts(type);
  struct node * ptr = (struct node *) malloc (sizeof(struct node));
  puts(type);
  memset(ptr, 0, sizeof(ptr));
  ptr=head;
  return head->id;
}
Run Code Online (Sandbox Code Playgroud)

这里node只是一个在链表中声明为节点的结构,它包含char *一个指向下一个节点的指针.但是,我意识到malloc()这里正在腐蚀我的输入char * foo.

为什么会malloc()破坏我的输入字符指针?另外,我怎么能在这里解决这个问题?现在我只是将该指针的内容复制到本地数组,但这太过于hacky,即使是我的口味(这不是最好的).

感谢您的任何投入!

编辑:嗯,这是更真实的代码:

void foobar(char * type) {
  puts(type); <-- here it's a long string about 30 char
  struct node * ptr = (struct node *) malloc (sizeof(struct node));
  puts(type); <- chopped of, 10 left with some random thing at …
Run Code Online (Sandbox Code Playgroud)

c memory malloc pointers corruption

0
推荐指数
2
解决办法
297
查看次数

冷凝开关声明?

我曾经想过一段时间,因为它似乎在我的经验丰富的代码中出现了很多.

我有一些代码使用了很多switch语句,但它真正做的就是每次都访问一个不同的队列.

void store(int toSwitchOn, float posx, float posy){ 
    myDataStruct newValue;
    newValue.psX = posx;
    newValue.psY = posy;

    switch(toSwitchOn){
        case 1:
            queue1.push(newValue);          
            break;
        case 2:
            queue2.push(newValue);          
            break;
        case 3:
            queue3.push(newValue);
            break;
        case 4:
            queue4.push(newValue);
            break;
        case 5:
            queue5.push(newValue);
            break;
    }


}
Run Code Online (Sandbox Code Playgroud)

每个语句中唯一改变的是队列变量.是否有一些巧妙的方法来压缩这种重复的代码?

c++ switch-statement

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