小编Ben*_*igt的帖子

在 C 和 C++ 中更改控制台输出的背景颜色

我使用“system”命令更改了控制台中的背景和文本颜色。

    #include <iostream>

using namespace std;

int main()
{
system ("color 1a");
cout <<"Hello World";

cin.ignore();
return 0;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法只在一行中改变颜色?C 或 C++ 都很好。谢谢。

c c++

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

将2-D数组传递给函数会产生奇怪的错误

嗨,我正在尝试创建一个函数,但它给出了错误

  [Error] cannot convert 'float (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'float (*)[30]' for argument '1' to 'float vecino_mas_cercano(float (*)[30])'
Run Code Online (Sandbox Code Playgroud)

我的代码是:

#include <iostream>
#include<stdio.h>
#include <fstream>
#include<stdlib.h>
using namespace std;

int n1=30;

float nearest_neighbor(float distances[30][30])
{  float min=distances[0][1];
   int candidates[n1];
   int city=0;

   for (int i=0;i<n1;i++)
   {  candidates[i]=i;
   }

   for (int i=0;i<n1;i++)
   {  for (int &j:candidates)
      {  if (j!=0)
            if (distances[city][j]<min);
            {  min=distances[i][j];
               candidates[i]=0;
               city=j;
            }
      }
   }
   return min;
}

int main()
{
   //Declaración de variables

   int n=30; //número de …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers function

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

Char和int16数组元素都显示为32位十六进制?

在下面的示例中:

int main(int argc, char *argv[])
{
    int16_t array1[] = {0xffff,0xffff,0xffff,0xffff};
    char array2[] = {0xff,0xff,0xff,0xff};
    printf("Char size: %d \nint16_t size: %d \n", sizeof(char), sizeof(int16_t));

    if (*array1 == *array2)
        printf("They are the same \n");
    if (array1[0] == array2[0])
        printf("They are the same \n");

    printf("%x \n", array1[0]);
    printf("%x \n", *array1);

    printf("%x \n", array2[0]);
    printf("%x \n", *array2);
}
Run Code Online (Sandbox Code Playgroud)

输出:

Char size: 1 
int16_t size: 2 
They are the same 
They are the same 
ffffffff 
ffffffff 
ffffffff 
ffffffff
Run Code Online (Sandbox Code Playgroud)

32位值为什么印刷两种charint16_t,为什么他们能进行比较和被认为是一样的吗?

c variadic-functions integer-promotion

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

Microsoft Media Foundation SDK示例下载

我很抱歉地问这样的问题,但现在它2小时因为我试图找到微软媒体基金会SDK样本.我已经安装了最新的Microsoft SDK 8.1和Microsoft SDK 10,并且没有MF SDK示例.来自MSDN的链接(例如此处的链接)不再起作用.

请你帮助我好吗?

media sdk sample ms-media-foundation

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

Try/Finally实际上是异常安全的吗?

假设你有一段代码如下:

resource = allocateResource();
try { /* dangerous code here  */ }
finally { free(resource); }
Run Code Online (Sandbox Code Playgroud)

我在这里没有提到任何特定的语言,但我猜Java,C#和C++就是很好的例子(假设你在MSVC++中使用__try/ __finally).

这个例外安全吗?

就个人而言,我不认为这是异常安全的,因为如果进入try之前有异常怎么办?那你的资源就会泄漏.

不过,我已经看过很多次了,我觉得我错过了什么......是吗?或者这真的不安全吗?


编辑:

不是要求allocateResource抛出异常,而是该函数返回之后但 resource分配之前得到异常的情况.

c# java exception-handling exception-safe visual-c++

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

三元运算符中的Cout String和int

我的c++代码中的一行是:

cout<<(i%3==0 ? "Hello\n" : i) ;//where `i` is an integer.
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

operands to ?: have different types 'const char*' and 'int
Run Code Online (Sandbox Code Playgroud)

如何修改代码(最少字符)?

c++ string int

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

在调用malloc后,我得到一个意想不到的大小

我有这个代码:

char* str = (char*)malloc(27);
int num = strlen(str);
Run Code Online (Sandbox Code Playgroud)

我已经在debag模式下运行程序,我已经看到num等于40.为什么?为什么不是27?

c++ size malloc

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

std :: map,带有struct error的值

我使用Visual Studio Express 2013,我尝试运行此代码:

struct opcode {
    int length; 
};

std::map<int, struct opcode> opcodes;

opcodes[0x20] = {
    3
};
Run Code Online (Sandbox Code Playgroud)

我收到此错误: error C2040: 'opcodes' : 'int [32]' differs in levels of indirection from 'std::map<int,opcode,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'

当我盘旋过来时,opcodes我得到了这个this declaration has no storage class or type specifier.

我的问题是我把声明放在了函数之外.

c++ visual-c++ c++11 visual-studio-2013

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

哪个更快的C++ String length()或size()?

length()返回字符串中的字符数并size()返回一个size_t也是相同但用于使其与其他STL容器一致的字符数.

对于计算length(),字符串遍历所有字符并计算长度.所以,O(n)时间.

是否size()也一样呢?

或者可以及时直接计算变量的大小O(1)

所以,我的问题是,它们在速度方面是否相同(如计算它们的方式)还是在O(1)时间上计算尺寸?

c++ string size string-length time-complexity

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

为什么这个矩阵乘法代码不起作用

#include<stdio.h>
#include<conio.h>
int main()
{
int ar1[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
int ar2[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int ar3[3][3];
int i,j,k;
for(i=0;i<3;i++)
{
    ar3[i][j] = 0;
    for(j=0;j<3;j++)
    {
        for(k=0;k<3;k++)
        {
            ar3[i][j] = ar3[i][j]+(ar1[i][k]*ar2[k][j]);
        }
    }
}
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++);
    printf("%d\t",ar3[i][j]);
}
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我在Dev C++中编译代码时,它没有给出任何错误但是无法运行并且应用程序停止工作.它出什么问题了?

c c++ arrays multidimensional-array matrix-multiplication

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

如何在单个C#函数中为两个或多个不同的变量使用相同的名称?

如何删除曾经声明和定义的变量?

例如,在C++中我会这样做:

int x;

..

delete x;
Run Code Online (Sandbox Code Playgroud)

我怎么能在C#中做到这一点?

(我需要做这样的事情:

switch (something)
{
case 1:
int Number;
break;

case 2:
float Number;
break;
}
Run Code Online (Sandbox Code Playgroud)

但是我不能这样做,因为Number已经被case 1占用了...而且我想要相同的名字,所以我想在声明float Number之前删除int Number,所以compilator不会对我大喊大叫.,p

c# variables scope identifier

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