我使用“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++ 都很好。谢谢。
嗨,我正在尝试创建一个函数,但它给出了错误
[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) 在下面的示例中:
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位值为什么印刷两种char及int16_t,为什么他们能进行比较和被认为是一样的吗?
假设你有一段代码如下:
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++代码中的一行是:
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)
如何修改代码(最少字符)?
我有这个代码:
char* str = (char*)malloc(27);
int num = strlen(str);
Run Code Online (Sandbox Code Playgroud)
我已经在debag模式下运行程序,我已经看到num等于40.为什么?为什么不是27?
我使用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.
解
我的问题是我把声明放在了函数之外.
length()返回字符串中的字符数并size()返回一个size_t也是相同但用于使其与其他STL容器一致的字符数.
对于计算length(),字符串遍历所有字符并计算长度.所以,O(n)时间.
是否size()也一样呢?
或者可以及时直接计算变量的大小O(1)?
所以,我的问题是,它们在速度方面是否相同(如计算它们的方式)还是在O(1)时间上计算尺寸?
#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++中我会这样做:
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