我目前是 C++ 的初学者,从 C 转过来。但我仍然是编程代码的初学者。
我目前正在寻找可以帮助我找到源代码问题的人(实际上,它来自一个朋友)。源代码似乎在我朋友的编译器 (Windows) 上运行良好,但我目前在 MacOS Big Sur 上使用 VSCode。
#include <stdio.h>
#include <iostream>
using namespace std;
struct Data_mhs{
char nim[12];
char nilai;
};
struct mhs{
Data_mhs data;
mhs* next;
};
mhs* head;
mhs* tail;
mhs* baru;
mhs* del;
void inisialisasi(){
head=NULL;
tail=NULL;
}
void input(Data_mhs br)
{
baru->data = br; //penugasan struktur
baru->next = NULL;
if(head==NULL)
{
head = baru;
tail = head;
}
else
{
tail->next = baru;
tail=baru;
}
}
void hapus(){
mhs simpan; …
Run Code Online (Sandbox Code Playgroud) 我正在学习使用 C++ 编码。我了解到,要使用string
数据类型,您需要包含该<string>
库,但是当我没有包含该<string>
库时,此代码仍然如何工作?
#include <iostream>
int main() {
std::cout << "Hello World!\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它会将 a 输出string
到控制台,而无需我包含该<string>
库。不应该返回错误吗?
在我看来,p{n}
代表this.p=n
和n(a)
手段this.n=this.a
,是这样吗?
我已经定义了一个像task1(int a,int n):a{a},n(n)...
. 为什么我a=1,n=2
在输入1,2
和假设时会得到输出this.a=1,this.n=2
?
class task1 {
public:
int a;
int n;
int p, q;
task1(int a, int n) :a(a), n(a), p{ n }, q(n){
cout << a << " " << n << " " << p << " " << q << endl;
}
};
void main() {
task1 t1 = task1(1, 2);
}
Run Code Online (Sandbox Code Playgroud)
我输入1 2
并假设它输出1 1 2 …
我试图将文件的内容打印到控制台,但它显示的是地址。请帮帮我(另外,请提出另一种方式而不是std::cout
,因为我们在课堂上没有研究过,我不明白,但我想我应该使用它)。
这是代码:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream f("board.txt");
if(f.is_open())
{
cout<<f;
}
else{
cout<<"ERROR:no file opened.";
}
}
Run Code Online (Sandbox Code Playgroud) 我正在编写代码,不小心int
在变量之前放置了两次,并注意到最终产品中存在一些不同。
int main ()
{
int number = 123456789;
int step = 0;
while (number>0)
{
cout << number%10 << endl;
number = number/10;
step = step+1;
cout << "step ; " << step << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
int main ()
{
int number = 123456789;
int step = 0;
while (number>0)
{
cout << number%10 << endl;
number = number/10;
step = step+1;
cout << "step ; " << step << endl;
}
return …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用该函数从文本文件中读取 3 行getline()
。
当我读取两行时,它工作得很好,因为我能够通过计算它是奇数行还是偶数行来区分。但对于 3 条线来说,这是不可能的。那么,有什么方法可以使用该getline()
功能来做到这一点吗?
这是数据在文本文件中的样子:
我想以每组 3 行代表代码中一个单独的节点的方式阅读它。因此,节点的第一行是联系人的姓名,节点的第二行是联系人组,第三行是电话号码。这样,我就可以将多个节点的数据存储在一个系列中。
void reopenCB() {
bool isEmpty;
ifstream myfile("contactbook.txt");
if (myfile.is_open() & myfile.peek() != EOF) {
int i = 0;
while (getline(myfile, x)) {
if (i % 2 == 0) {
if (head == NULL) {
Node *newer = new Node;
newer->name = x;
newer->next = NULL;
newer->prev == NULL;
head = newer;
} else {
Node *newer = new Node;
newer->name = x;
newer->next = NULL;
Node …
Run Code Online (Sandbox Code Playgroud) 我正在学习高级主题,并在脑海中遇到一个问题 - 为什么我需要使用按值传递而不是按引用传递?
问题源于内存管理,也就是说,如果将变量作为值传递到函数中会生成该变量的副本,我是否应该需要通过引用将其作为 const 传递以有效地使用内存?
当我在 Google 上进行研究时,它通常指出,当我们不想更改函数内部的变量时,应该使用值传递。
下面是我的例子:
#include <iostream>
void foo(int a) {
std::cout << "value of a: " << a << std::endl;
}
int main(){
int b = 5;
foo(5);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码块中,我将b
值传递给 function foo()
。其中复制了b
.
相反,如果我编写如下所示的函数:
#include <iostream>
void foo(const int& a) {
std::cout << "value of a: " << a << std::endl;
}
int main(){
int b = 5;
foo(5);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
使用此代码块,我不会复制b
.
除了当我不想更改函数内部变量的内容时,当我调用函数而不是按引用传递时,使用按值传递有什么优点?
我很好奇为什么操作2可以成功交换a
和b
,而操作1却不能。
//operation1:
void exchange(char *a, char *b)
{swap(a,b);}
//operation2:
swap(a,b);
Run Code Online (Sandbox Code Playgroud)
(我已将整个代码压缩为上面的几行)
我意识到操作1有点无稽之谈,但我仍然想知道为什么它不起作用。
我有一个数组,MyStruct* my[100];
我在循环中设置数据,如下所示:
if id > X && id < Y { my[id-X] = p;
Run Code Online (Sandbox Code Playgroud)
现在我想访问my[0]
as name0
、my[1]
as different1
、my[2]
asanotherThing
等。
我该怎么做呢?我尝试了union+X
宏,但这在 gcc 中不起作用(它在 clang 中起作用)。
我想我必须编写一个脚本,这样我就不会手写所有的#defines
,但我认为#define anotherThing my[2]
这是给出数组项名称的唯一方法,对吗?我想简单地写different1->myStructMember
,而不是my[123]->myStructMember
每次都写,这样不可读。
我在我的程序中实现了 3 个辅助线程。
GET
第一个通过每 2 分钟向每个外部设备发送一个 HTTP 请求来检查外部设备的在线状态。
第二个用于“实时查看”功耗,每 2 秒检查一次外部设备的值,并在主线程中使用一些进度条/标签显示它们。
第三个等待并每隔一小时从同一设备获取其他值并将它们写入文件。
在线程 #3 中没有Synchronize()
必要(我认为),因为主线程获取文件并根据用户命令从中构建图表。
实际上,所有线程都像魅力一样运行几个小时,直到午夜。第一个线程 24/7 持续运行,没有任何问题,但其他两个线程似乎在午夜锁定。
主窗口始终保持功能状态。
如果我关闭程序并重新启动它,一切都会再次正常工作,但在 00:00h ... finito 丢失了线程。
难道是程序中的等待循环有问题Execute()
?到目前为止我还没有找到。
我是第一次来,如有遗漏或不清楚的地方,请多多包涵!
这是线程 #1 的声明和实现:
type
TpingThread = class(TThread)
private
pingHTTP: TidHTTP;
public
constructor Create;
procedure updateOnlineStatus;
protected
procedure Execute; override;
end;
constructor TPingThread.Create;
begin
Self.Suspended := False;
Self.FreeOnTerminate := False;
inherited Create(False);
end;
//"pings" all networkdevices, runs until program ends
procedure TPingThread.Execute;
Var delayTime: TDateTime;
n: Byte;
s: String;
begin …
Run Code Online (Sandbox Code Playgroud)