我写了一个小程序来习惯memset()操作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int main()
{
int arr[10], i;
int t = INT_MAX;
memset(arr, t, sizeof(arr));
for (i = 0; i < 10; i++)
printf("%d\t",arr[i]);
printf("%d",t);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上述计划的结果是:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2147483647
Run Code Online (Sandbox Code Playgroud)
memset()上述程序的行为是什么?为什么要将数组元素设置为-1?
为什么在头文件中定义类的功能是不好的做法?
让我说我有一个头文件,我在类定义本身定义类的功能,如,
headerfile.hpp
#ifndef _HEADER_FILE_
#define _HEADER_FILE_
class node{
int i;
public:
int nextn(){
......
return i;
}
}
#endif //_HEADER_FILE_
Run Code Online (Sandbox Code Playgroud)
因此在类中定义函数就像这样使函数"内联".所以如果我们在两个.cpp文件中包含这个头文件,它会导致"多重定义错误"吗?定义像这样的函数是不好的做法这在课程定义中?
我一直在尝试c中的字符串.在这段代码中.
#include<stdio.h>
int main()
{
char *arr="output";
*arr='s';
printf("%s",arr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在内存中,字符串"output"被创建,并且因为我们有指针arr(位于堆栈中),它最初指向此字符串,为什么不能为指针分配一些其他字符?当我试图运行这个程序时,我看到运行时错误信号:11这是分段错误.
我已经了解到,在c ++中,字符串"output"在只读内存中创建,导致在编译过程中不允许将字符串常量转换为'char*'.在c中的行为是什么?
有人能解释一下为什么会导致分段错误吗?这个字符串"输出"在哪里首先创建.
谢谢.
我在接受采访时遇到过这个问题.
给定二叉树,找到最多一个转弯的最长路径长度.路径的一端必须是一片叶子.另一端可以是叶子或任何节点.
转弯定义为:
In tree1-> start from 1 and there is a turn at root 2 towards right,
In tree2-> starts from 3 goes in left and there is a turn at 1 towards right ,
In tree3-> starts from 1 goes in right and there is a turn at 3 towards left,
2 3 1
/ \ / \
1 3 1 3
\ /
2 2
Run Code Online (Sandbox Code Playgroud)
一些人可以帮助如何继续.谢谢..
编辑:在采访中,我被问到这个问题是树问题直径的后续问题.
我对树的直径的实现是这样的.
变量'res'包含最终答案.
int maxPathSumUtil(struct Node *root, int &res) …Run Code Online (Sandbox Code Playgroud) 我有以下要求,只有当给定的字符串以"Y"或"Years"或"YEARS"结尾时,才需要做几件事.
我尝试使用像这样的正则表达式.
String text=1.5Y;
if(Pattern.matches("Y$",text) || Pattern.matches("YEARS$",text) || Pattern.matches("Years",text))
{
//do
}
Run Code Online (Sandbox Code Playgroud)
然而,这是失败的.
有人可以指出我出错的地方或建议我任何其他可行的方法.
编辑:
谢谢.这有帮助.
最后我用过了 "(?i)^.*Y(ears)?$| (?i)^.*M(onths)?$".
但我想做出更多改变以使其完美.
比方说我有很多字符串.
理想情况下,如果检查,只能通过1.5Y或0.5-3.5Y或2.5/2.5-4.5Y等字符串.
It can be number of years(Ex:2.5y) or the period of years(2.5-3.5y) or the no of years/period of years(Ex.2.5/3.5-4.5Y) nothing more.
More Examples:
--------------
Y -should fail;
MY - should fail;
1.5CY - should fail;
1.5Y-2.5Y should fail;
1.5-2.5Y should pass;
1.5Y/2.5-3.5Y should fail;
1.5/2.5-3.5Y should pass;
Run Code Online (Sandbox Code Playgroud) 我正在读一本书,在那里我找到了这个问题
提到数组名称是否给出了所有上下文中的基址?
有人可以解释数组名称不提供基址的情况.谢谢
我最近看过一个小c程序.在那个程序中,结构是以这种方式声明的,我无法理解.
struct
{
mynode *node;
unsigned vleft :1;
unsigned vright :1;
}save[100];
Run Code Online (Sandbox Code Playgroud)
这里node是指向其他结构的指针.
有人可以解释一下unsigned vleft:1; unsigned vright:1; 是?而且我找不到任何分配给vleft和vright的数据类型.这是什么原因?
谢谢.
我正在阅读一些c ++的赋值运算符理论.
让我们说吧
class MyClass {
private:
T1 member1;
T2 member2;
public:
// The default copy assignment operator which assigns an object via memberwise copy
MyClass & operator=(const MyClass & rhs) {
member1 = rhs.member1;
member2 = rhs.member2;
return *this;
}
......
}
Run Code Online (Sandbox Code Playgroud)
和
c7 = c6; //member wise copy assignment.
Run Code Online (Sandbox Code Playgroud)
这里我们在赋值操作期间返回对象的引用,并为其指定新对象c7.
但是,如果我的代码有点像这样:
int a=12;
int &b=a;
int c=&b; //error::invalid conversion from ‘int*’ to ‘int’
Run Code Online (Sandbox Code Playgroud)
为什么这与上述情况不同?
我是数据库新手。
可以说我已经用力创建了一个视图。
那么如何知道创建的视图是否无效呢?
我的意思是,是否有任何查询可以让我了解视图的有效性状态?
谢谢。
我是c ++的新手.
#include<cstdio>
#include<string>
using namespace std;
class add{
public :
int a,b;
add();
add(int ,int);
add operator+(add);
};
add::add():a(0),b(0){};
add::add(int x,int y):a(x),b(y){};
add add::operator+(add z)
{
add temp;
temp.a=a+z.a;
temp.b=b+z.b;
return temp;
}
int main()
{
add har(2,5),nad(3,4);
add total;
total=har+nad;
cout<< total.a << " "<<total.b;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个程序现在工作正常.但是,我早些时候写过
temp.a=this.a+z.a;
temp.b=this.b+z.b;
Run Code Online (Sandbox Code Playgroud)
考虑到调用与编译时total=har+nad;相同total=har.operator+(nad);,显示错误.
operover1.cpp: In member function ‘add add::operator+(add)’:
operover1.cpp:22:14: error: request for member ‘a’ in ‘this’, which is of non-class type ‘add* const’
operover1.cpp:23:14: …Run Code Online (Sandbox Code Playgroud)