当我写这段代码时:
polly = "alive"
palin = ["parrot", polly]
print(palin)
polly = "dead"
print(palin)
Run Code Online (Sandbox Code Playgroud)
我以为它会输出这个:
"['parrot', 'alive']"
"['parrot', 'dead']"
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.如何输出它?
可能重复:
为什么我的程序在完全循环8192个元素时会变慢?
我一直在修补一个程序,我用它来简单地总结一个二维数组的元素.一个错字导致了至少在我看来,一些非常奇怪的结果.
处理数组时,矩阵[SIZE] [SIZE]:
for(int row = 0; row < SIZE; ++row)
for(int col = 0; col < SIZE; ++col)
sum1 += matrix[row][col];
Run Code Online (Sandbox Code Playgroud)
运行得非常快,但上面的行sum1 ...被修改:
sum2 += matrix[col][row]
Run Code Online (Sandbox Code Playgroud)
正如我在没有意识到的情况下偶然做过的那样,我注意到我的运行时间显着增加.为什么是这样?
是否有可能改变行为if()
:
class Foo {
int x;
};
Foo foo;
if(foo)
Run Code Online (Sandbox Code Playgroud)
只有当值x
不是零时才进行?要么...
将显式的用户定义类型转换为int工作/将是一种合适的方法吗?要么...
最好做点什么if(foo.getX())
?
有什么区别:
void foo(item* list)
{
cout << list[xxx].string;
}
Run Code Online (Sandbox Code Playgroud)
和
void this(item list[])
{
cout << list[xxx].string;
}
Run Code Online (Sandbox Code Playgroud)
假设项目是:
struct item
{
char* string;
}
Run Code Online (Sandbox Code Playgroud)
指针指向字符数组的第一个
并且list
只是一系列项目......
当我尝试以这种形式编译代码时,会生成编译错误"不兼容的类型 - 找到java.lang.Object但是期望的E":
public class E {
private int x;
public E {
x = 0;
}
}
public class Class {
private E o;
private E b;
private Stack stack = new Stack();
public void foo() {
stack.push(o);
}
public void bar() {
**b = stack.pop();**
}
}
Run Code Online (Sandbox Code Playgroud) 我需要使用什么返回和变量类型来捕获传递给函数的变量的地址的返回值?
说:
RETURNTYPE get_address(int num){
return #
}
int main(){
int num = 1;
DATATYPE = get_address(num);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 当我尝试传入一个数组作为参数时,我得到:
"No matching function to call to ' table::retrieve(const char[16], item&, int)'
Run Code Online (Sandbox Code Playgroud)
我试图用这个函数调用
program.reference.retrieve("Abecean Longfin", program.client_item, 1);
Run Code Online (Sandbox Code Playgroud)
功能是
int table::retrieve(char item_in[],item*item_list, int name_flag)
Run Code Online (Sandbox Code Playgroud)
我确信这是一件我不理解的简单事情,但我是新手.
for (i = 0; i < n; i++)
{
x[i] = (float) (i * step);
k = 5;
sum = 0;
while(k > 0)
{
sum = sum + (1/k) * sin((k*PI*x[i])/5);
k = k - 2;
}
y1[i] = (4/PI)*sum;
y2[i] = 0*(4/PI)*sin((PI*x[i])/5);
}
Run Code Online (Sandbox Code Playgroud)
当调试除1之外的每个k值时,总和显示为等于0,我是否正确实现了循环?
编辑1:
int k;
double sum;
Run Code Online (Sandbox Code Playgroud) 尝试构建包含以下代码的c ++程序时:
menutype::menutype(int cat_num){
extras list = extras(cat_num);
}
extras::extras(int num_cats){
head = new category_node;
head->next = NULL;
head->category = 1;
category_node * temp;
for(int i = 1; i < (num_cats); ++i){
temp = new category_node;
temp->next = head->next;
head->next = temp;
temp->category = (num_cats-(i-1));
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
cs163hw1.cpp:在构造函数'menutype :: menutype(int)'中:
cs163hw1.cpp:59:31:错误:没有匹配函数来调用'extras :: extras()'
cs163hw1.cpp:59:31:注意:候选人是:
cs163hw1.cpp:5:1:注意:extras :: extras(int)
我不明白为什么,请帮忙!
想知道如何分配指向数组成员的指针:
struct foo {
int INT;
}
int main() {
foo bar[10];
foo *baz;
baz = bar[5];
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,但我想知道会发生什么.非常感谢您的帮助.
c++ ×7
arrays ×4
pointers ×3
class ×2
object ×2
parameters ×2
c ×1
constructor ×1
for-loop ×1
if-statement ×1
java ×1
list ×1
python ×1
return ×1
stack ×1
struct ×1
sum ×1
variables ×1
while-loop ×1