我想知道你将如何解决这个问题:
我有一节课Foo:
class Foo
{
public:
Foo() { }
~Foo() { }
float member1() { return _member1; }
private:
float _member1;
// other members etc...
}
Run Code Online (Sandbox Code Playgroud)
一个容器类,除其他外,它包含一个指向Foo实例的指针容器
class FooContainer
{
public:
FooContainer() { }
~FooContainer() { }
void addFoo(Foo* f) {_foos.push_back(f);}
private:
boost::ptr_vector<Foo> _foos;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:在运行时我需要向Foo"添加"新的(完全不同的)成员,具体取决于GUI的指令.我可以通过创建这样的两个"装饰器"来解决这个问题:
class Decorator1
{
public:
int alpha() { return _alpha; }
float beta() { return _beta; }
private:
int _alpha;
float _beta;
}
class Decorator2
{
typedef std::complex<float> cmplx;
public:
cmplx gamma() { …Run Code Online (Sandbox Code Playgroud) 此代码给出整数的最小除数.但问题是我必须计算平方根.有没有办法让我不必明确计算平方根?
int d,r,n;
scanf("%d",&n);
if(n%2==0)
{
printf("2 is ans");
}
else
{
r=sqrt(n);
d=3;
while((n%d!=0)&&d<r)
{
d=d+2;
}
if(n%d==0)
printf("ans is %d",d);
else
printf("ans is 1");
}
Run Code Online (Sandbox Code Playgroud) 怎么(int[]) (int[])办?
private Object[] slots = new Object[8192];
int[] block = (int[]) (int[]) this.slots[0];
Run Code Online (Sandbox Code Playgroud) 我有以下类型的注释:
public @interface A {
String[] oneArr();
String[][] twoArr();
}
Run Code Online (Sandbox Code Playgroud)
为什么第一种类型String[] oneArr();有效,但第二种类型 String[][] twoArr();无效?
我正在研究一个问题,要求我在字典中返回最不频繁的值,除了几个不同的计数之外我似乎无法解决它,但字典中没有一定数量的值在支票中提供.
例如,假设字典包含从学生姓名(字符串)到其年龄(整数)的映射.您的方法将返回最不常见的年龄.考虑包含以下键/值对的字典变量d:
{'Alyssa':22,'Char':25,'Dan':25,'Jeff':20,'Kasey':20,'Kim':20,'Mogran':25,'Ryan':25,'燕姿":22}
三个人年龄为20岁(Jeff,Kasey和Kim),两个人年龄为22岁(Alyssa和Stef),四个人年龄为25岁(Char,Dan,Mogran和Ryan).最稀有的(d)返回22,因为只有两个人是那个年龄.
有人会介意我指出正确的方向吗?谢谢!
给出了一个在所有三个维度中排序的 3d 矩阵,我们必须在其中找到一个给定的数字。
对于上述问题,我一直在思考:3D 数组arr[m][n][r]就像一堆矩形,其中每个矩形(考虑arr[m][n][0])将最大元素作为最低的最右边元素(arr[m-1][n-1][0])。我们可以在 中的每个矩形内搜索O(m+n) :
int row = 0;
int col = N-1;
while (row < M && col >= 0)
{
if (mat[row][col] == elem)
{
return true;
}
else if (mat[row][col] > elem)
{
col--;
}
else
{
row++;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为它可以类似地扩展到第三维,因此使其成为线性复杂度解决方案(O(m+n+r))。我对吗 ?
有没有人有其他想法?复杂性如何?
我试图在Windows 7中使用Eclipse创建我的第一个C++可执行程序.
#include <stdio.h>
int main( int argc, const char* argv[] )
{
printf( "arg" );
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
"'printf'这个词拼写错误".
以下是我的配置.如何克服这个错误?



出于好奇,我将行更改为set_array(&array1[0])以下内容set_array[array1],参数不是同一类型,但它有效,任何想法?
#include <stdio.h>
void set_array(int array[][9]);
int main(void) {
int array1[4][9];
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 9; j++) {
array1[i][j] = j + 1;
}
}
set_array(&array1[0]);
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 9; j++) {
printf("%d ", *(*(array1 + i) + j));
//printf("%d ", array1[i][j]);
}
puts("\n");
}
return 0;
}
void set_array(int array[][9]) {
for(int …Run Code Online (Sandbox Code Playgroud) 如果你有两对值,开始和结束 - 你如何计算它们的重叠位置?
即,如果开始和结束值对是
[10,20],[15,20]
在这种情况下compute_overlap((15,20),(10,20))应该返回,(15,20)因为这是重叠的地方.
做这个的最好方式是什么?
我在c中这样做了:
#include<stdio.h>
int main (void)
{
int n,i;
scanf("%d", &n);
for(i=2;i<=n;i=i+2)
{
if((i*i)%2==0 && (i*i)<= n)
printf("%d \n",(i*i));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
什么是更好/更快的方法来解决这个问题?
关于将数组传递给函数我有疑问.
请考虑以下代码段.
void main()
{
int a[4]={10,20,30,40};
fun1(a);
}
void fun1(int a1[])
{
for(int i=0;i<4;i++)
{
printf("%d\n",a1[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
传递数组只不过是传递第一个位置的地址.我应该传递上面的数组及其名称(数组的起始地址).我怀疑是因为[4]是一个自动变量,它应该在它从主函数出来时死掉它应该给出意想不到的结果(指针应该是悬空的).但它工作正常.
我对此非常困惑,请你把它清理干净.
即使我们通过一个单一的元素int a如f(&a),它不应该在函数f存在,如果它被声明为自动(在主函数的局部变量).
请清楚这一点.
例如,
我有一个类foo与一些成员函数..
在某些功能中说..
void foo::someFunction()
{
int pointer = ??????
}
Run Code Online (Sandbox Code Playgroud)
如何传递指向对象foo的指针?
我能这样做吗?或者我必须在外部进行吗?