我写了一个程序来用星号填充封闭的数字。
出于某种原因,它不接受哨兵值EOF Ctrl+ D。
为什么是这样?
#include "usefunc.h"
#define height 100
#define width 100
void showRow(int numbers[], int size_numbers) {
int i;
printf("[ ");
for (i = 0; i < size_numbers-3; i++) {
printf("%c, ", numbers[i]);
}
printf("%c ]", numbers[size_numbers-3]);
printf("\n");
}
void showshape(int shape[][width], int lines, int max_buf) {
int i, j;
for (i = 0; i < lines; i++) {
for (j = 0; j < max_buf; j++) {
printf("%c", shape[i][j]);
}
printf("\n");
}
}
void …Run Code Online (Sandbox Code Playgroud) 我正在尝试将string十进制数转换为double,但是当我使用该atof()函数时,我的数字最终会四舍五入为整数。
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string num = "135427.7000";
double r = atof(num.c_str());
cout << r << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
135428
Run Code Online (Sandbox Code Playgroud)
我想要:
135427.7
Run Code Online (Sandbox Code Playgroud) 我刚刚了解了向量,但对它们的使用感到困惑。
请告诉我两者之间有什么区别:
vector<int> a;
Run Code Online (Sandbox Code Playgroud)
,
vector<int> a[n];
Run Code Online (Sandbox Code Playgroud)
和
vector<int> a(n);
Run Code Online (Sandbox Code Playgroud) 我已经编写了这段代码,但是当我尝试初始化一个Critter对象数组并且不知道它们是关于什么时出现了一些错误。
我的代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Critter {
private:
string crName;
public:
Critter(string = "Poochie");
string getName() const { return crName; }
};
Critter::Critter(string n) {
crName = n;
}
int main() {
Critter c[10] = { "bob","neo","judy","patrik","popo" }; //here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Critter {
private:
string crName;
public:
Critter(string = "Poochie");
string getName() const { return crName; }
};
Critter::Critter(string …Run Code Online (Sandbox Code Playgroud) c++ arrays runtime-error visual-studio object-initialization
这段代码:
int scores[] {1,2,3,4};
int *score_ptr {scores};
//let's say that initial value of score_ptr is 1000
std::cout<<*score_ptr++;
Run Code Online (Sandbox Code Playgroud)
产生输出:
1
Run Code Online (Sandbox Code Playgroud)
As*和++具有相同的优先级,然后结合性是从右到左,我们不应该++先应用运算符,即先增加指针然后*(取消引用)它吗?
因此,相应地score_ptr将增加到1004然后取消引用它将给出分数的第二个元素,即2.
这如何以及为什么给我输出1而不是2?
以下代码的输出是0.0000000:
#include <stdio.h>
int main() {
float x;
x = (float)3.3 == 3.3;
printf("%f", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
而此代码输出1.000000:
int main() {
float x;
x = (float)3.5 == 3.5;
printf("%f", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
2个代码唯一的区别是比较中的值,但是结果不一样,这是为什么呢?
我正在编写排序可视化。我希望它在算法运行时处理事件和更新屏幕。我有一个具有 SortContext 属性的 Controller 类。我希望 SortContext 的排序方法引用控制器,以便它可以使用方法update,isAppRunning但出现错误:
控制器尚未声明
我怎样才能让它工作?
控制器.hpp
class Controller {
private:
SortContext context;
View view;
std::vector<Sortable> data;
bool algorithmRunning = false;
void handleEvents();
public:
Controller();
void update();
bool isAppRunning();
};
Run Code Online (Sandbox Code Playgroud)
排序上下文.hpp
class SortContext {
private:
std::unique_ptr<SortStrategy> strategy;
static const std::vector<std::function<std::unique_ptr<SortStrategy>(void)>> algorithms;
public:
void chooseStrategy(int algorithmId);
void sort(std::vector<Sortable> &data, Controller &controller);
};
Run Code Online (Sandbox Code Playgroud) 我运行该程序,发现这些值并不总是 0 和 1。但这是一种bool数据类型,对吗?
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
bool arr[5][5];
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 据我所理解
int* p = new int();
Run Code Online (Sandbox Code Playgroud)
就像用构造函数创建 int 一样。如果是这样,为什么下面的代码像数组一样工作?
int* p = new int();
*p = 5;
p[1] = 15;
for (int i = 0; i < 2; i++)
cout << p[i] << endl;
Run Code Online (Sandbox Code Playgroud)
5
15
Run Code Online (Sandbox Code Playgroud) 我有这个交换通过引用传递的整数的函数,它在 C++ 中工作正常,但在 C 中不起作用。
#include <stdio.h>
void swap(int & x, int & y)
{
int z = x;
x = y;
y = z;
}
int main()
{
int a = 0, b = 1;
swap(a, b);
printf("a is now %d\n", a);
printf("b is now %d\n", b);
}
Run Code Online (Sandbox Code Playgroud)
为什么它在 C 中不起作用?