有谁请解释这个案例:为什么我在这个简单的代码中面临'双重免费'问题?
void Rreceive (myclass){}
int main () {
myclass msg (1);
Rreceive(msg);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在哪里myclass:
class myclass
{
private:
HeaderType header;
byte * text;
public:
myclass(int ID);
~myclass();
HeaderType getHeader();
byte * getText();
};
myclass::myclass(int ID){
header.mID = ID;
text = (byte *)malloc (10);
memset (text, '.', 10);
}
myclass::~myclass(){
free (text);
}
HeaderType myclass::getHeader(){
return header;
}
byte * myclass::getText(){
return text;
}
Run Code Online (Sandbox Code Playgroud)
和HeaderType是:
typedef struct {
int mID;
}HeaderType;
Run Code Online (Sandbox Code Playgroud)
错误是: *** glibc detected *** ./test: …
所以我正在编写这段代码,并使用了 switch 语句。我想要做的是,当选择任何无效选项时,程序应该显示一条消息,然后在一段时间后仅清除该消息,同时保持选项仍在屏幕上。在到目前为止的代码中,我使用 Windows.h 库的 Sleep 函数进行暂停,还使用 goto 返回询问选项,但似乎不知道如何从屏幕上删除错误消息。我无法使用,system("cls");因为在选择此选项之前我有一个菜单,有点像登录,所以我不希望它消失,直到选择了有效的选项。这是我的代码:
cout<<endl<<"\t\t\t\t\t\t - Access Denied! -"<<endl;
cout<<"\t\t\t\t\t\t + Press [1] To Try Again. +"<<endl;
cout<<"\t\t\t\t\t\t + Press [2] To Go Back. +"<<endl;
char TryAgain = ' ';
cout<<"\t\t\t\t\t\t >>[ ]<< ";
InvalidOption:
SetConsoleCursorPosition(hStdout, { 84, 14 });
cin>>TryAgain;
switch (TryAgain)
{
case '1':
goto LoginAgain;
case '2':
break;
default:
{
cout<<"\t\t\t\t\t Select A Valid Option!";
Sleep ( 450 );
cout << "\b";
goto InvalidOption;
}
}
Run Code Online (Sandbox Code Playgroud)
是的,我正在使用大量的/t,也许还有一堆其他的东西,但这只是我正在尝试的示例代码,而不是直接在我的原始项目上进行实验。
#include <iostream>
typedef union dbits {
double d;
struct {
unsigned int M1: 20;
unsigned int M2: 20;
unsigned int M3: 12;
unsigned int E: 11;
unsigned int s: 1;
};
};
int main(){
std::cout << "sizeof(dbits) = " << sizeof(dbits) << '\n';
}
Run Code Online (Sandbox Code Playgroud)
输出:sizeof(dbits) = 16,但是如果
typedef union dbits {
double d;
struct {
unsigned int M1: 12;
unsigned int M2: 20;
unsigned int M3: 20;
unsigned int E: 11;
unsigned int s: 1;
};
};
Run Code Online (Sandbox Code Playgroud)
输出: …
我是C++的初学者,并且使用unix.所以这是我的问题.
我在main-function中写了几行,我需要一个在c_lib库中定义的函数.
main.cpp中:
#include <iostream>
#include "c_lib.cpp"
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想在终端上执行它,所以我写道
g++ -c c_lib.cpp
g++ -c main.cpp
g++ -o run c_lib.o main.o
Run Code Online (Sandbox Code Playgroud)
在此之前,没有错误报告.
然后
./run
Run Code Online (Sandbox Code Playgroud)
我收到了错误
错误:./ run:没有这样的文件或目录
怎么了?
我有标题的问题,但如果没有,我怎么能只使用4位表示整数?
编辑真的我的问题是如何.我知道像c这样的语言有1个字节的数据结构,但是我怎么能用char之类的东西来存储两个整数呢?
这是我的代码:
#include <iostream>
using namespace std;
bool funkcja (char * a[3][7], char * b[7]);
int main()
{
char T[3][7]={{'A', 'L', 'A', 'M','A', 'k', 'o'},
{'M', 'A','M','K','O','T','A'},
{'T', 'E','Q','U','I','L','A'}};
char tab[7]={ 'A', 'L', 'A', 'M','A', 'k', 'o' };
cout<<funkcja(T, tab)<<endl;
return 0;
}
bool funkcja (char * a[3][7], char * b[7])
{
int licznik=0;
for (int i=0;i<3;i++)
{
for (int j=0; j<7;j++)
{
if (a[i][j]==b[j])
{
licznik++;
if (licznik==7) return true;
}
else {
licznik=0;
}
}
licznik=0;
}
return false; …Run Code Online (Sandbox Code Playgroud) 我这样做是因为我学习编程的网站,我根本无法解决这个问题.
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main ()
{
int n;
string thename;
vector <pair<string,int>> name;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> thename;
name.push_back(make_pair(thename, thename.length()));
}
sort(name.begin(), name.end());
sort(name.begin(), name.end(),
[](const pair<string, int>& lhs, const pair<string, int>& rhs) {
return lhs.second < rhs.second; } );
for (int i = 0; i < n; i++)
{
cout << name[i].first<< endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我的老人建议我不要使用lambda排序,因为我自己仍然不太了解它.但我仍会接受任何lambda排序的答案.谁能帮我?
假设我们有从[0]到[14]的15个数字满足:
a[0]=4
a[i]=pow(sqrt(a[i-1])*2-1, 2) for i>=1 and i<=14
Run Code Online (Sandbox Code Playgroud)
我需要打印出从[0]到[14]的所有值,每个数字在一行上,所以我写下面的代码(C++,Code :: Blocks):
#include <iostream>
#include <cmath>
using namespace std;
#define maxN 15
int i[maxN]; int n=15;
int main()
{
i[0]=2;
cout<<4<<endl;
for (int k=1; k<n; k++){
i[k]=i[k-1]*2-1;
cout<<pow(i[k],2)<<"\t"<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
4
9
25
81
289
1089
4225
16641
66049
263169
1.05062e+006
4.1984e+006
1.67854e+007
6.71252e+007
2.68468e+008
Run Code Online (Sandbox Code Playgroud)
最后五个数字不正确(因为整数溢出).
在上面的"for"循环中,我改变了这一行
cout<<pow(i[k],2)<<"\t"<<endl;
Run Code Online (Sandbox Code Playgroud)
至
cout<<(long) pow(i[k],2)<<"\t"<<endl;
Run Code Online (Sandbox Code Playgroud)
这一次,结果是:
4
9
24
81
289
1089
4224
16641
66048
263168
1050625
4198400
16785408
67125248
268468224 …Run Code Online (Sandbox Code Playgroud) 我在二分搜索实现上遇到了很多困难,尤其是
r和l)r=mid或r=mid-1我试图实施upper_boundfrom STL,但无法得到正确的答案。这是我的代码。
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> a(n+2);
// adding 0 in front to avoid out of bounds error
// when looking for a[m-1]
a[0]=0;
for(int i=1; i<=n; i++)
cin >> a[i];
// adding a big element at the end to return that index
// when required element is greater than all elements
a[n]=INT_MAX;
// q …Run Code Online (Sandbox Code Playgroud) 我们有3位数字(100-999).有多少这样的数字至少有一个数字"2"存在?
如何制作算法呢?还是数学函数?
c++ ×9
algorithm ×1
arrays ×1
bcd ×1
binary ×1
bit-fields ×1
c ×1
codeblocks ×1
combinations ×1
destructor ×1
fortran ×1
free ×1
math ×1
sorting ×1
unions ×1