通过定义存储单元
struct node {
int item;
node *next;
};
Run Code Online (Sandbox Code Playgroud)
假设ptr指向一个链表,那么put while(ptr!=NULL) 和 while(ptr->next!=NULL)循环通过列表之间是否有区别,直到到达空指针?
我有类似的东西
while(playAgain==true)
{
cout<<"new game"<<endl; //i know 'using namespace std;' is looked down upon
while(playerCard!=21)
{
*statements*
if(decision=='n')
{
break
}
...
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我想要打破两个循环时,那个中断只会突破第一个while循环
我写这篇文章是为了好玩:
#include <iostream>
#include <cstdlib>
using namespace std;
int Arsenal = rand()%3;
int Norwich = rand()%3;
int main () {
if (Arsenal > Norwich) {
cout << "Arsenal win the three points, they are in the Top Four";
return 0;
} else if (Arsenal == Norwich) {
cout << "The game was a draw, both teams gained a point";
return 0;
} else (Norwich > Arsenal) {
cout << "Norwich won, Arsenal lost";
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试用g ++编译它,但我得到这个错误:
arsenalNorwich.cpp: …Run Code Online (Sandbox Code Playgroud) void printInt(int n){
if(n==1)
cout<<1<<" ";
else
printInt(n-1);
cout<<n<<" ";
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是
1 1 2 3....n
Run Code Online (Sandbox Code Playgroud)
我在一张纸上写出了函数的实际步骤,但我不知道如何在控制台中打印额外的1(Visual Studio 2010).这是来自过去的硬件解决方案,所以这只是为了理解它是如何工作的.
到目前为止,我已经在C++上学过两门课,每门课都在不同的学校,并且他们都使用了'using namespace std;' 教基础编程.这可能是巧合,但我不得不自己去找出这样做不好的做法.
我将<string>库包含在我的.cpp文件的顶部,但是当我测试它时
cout<<myString.at(myString.length());
Run Code Online (Sandbox Code Playgroud)
它应该打印出字符串的最后一个字母,或者至少我认为它应该打印出来.但是我的编译器抛出一个怪异的东西,并向我吐出一堆行话.
我习惯用JavaScript写作,所以我不习惯,好吧....有规则,所以对我来说这很有必要返回字符串的最后一个字符.
c++ ×6
while-loop ×2
break ×1
if-statement ×1
linked-list ×1
loops ×1
recursion ×1
std ×1
string ×1