错误如下:
请求'arr'中的成员'begin','end'是非类型int [5],无法从表达式错误中推断出来.
我的代码:
#include <iostream>
using namespace std;
int main()
{
int * mypointer;
int arr[5] = {1,3,5,7,9};
mypointer = arr;
for(auto it = arr.begin(); it != arr.end(); ++it) {
cout<<*mypointer<<endl;
mypointer++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 假设我有以下,这是更好,更快,更Pythonic方法,为什么?
if x == 2 or x == 3 or x == 4:
do following...
Run Code Online (Sandbox Code Playgroud)
要么 :
if x in (2, 3, 4):
do following...
Run Code Online (Sandbox Code Playgroud) 此程序修改类"myclass"_x和_y的对象,但我不将其作为参数传递给函数try_block.如何修改对象?我是Python的新手.
class AddSub:
def _init_(self): #how do default parameters work?
self._x, _y
def set_x(self, num):
self._x = num
def set_y(self, num):
self._y = num
def add(self):
return self._x + self._y
def sub(self):
return self._x - self._y
def try_block():
try:
ch = int(input("type 1 to add, and 2 to subtract: "))
myclass.set_x(int(input("enter an x value: "))) #how does myclass get modifed?
myclass.set_y(int(input("enter a y value: ")))
return ch
except ValueError:
print("Invalid entry.")
ch = try_block()
return ch
myclass = AddSub()
choice …Run Code Online (Sandbox Code Playgroud) class Student:
def __init__(self, name="empty", year=0, GPA=1.0):
self.name = name
self.year = year
self.GPA = GPA
def __str__(self):
return "{0} is in year {1}, with a GPA of {2}.".format(self.name, self.year, self.GPA)
Run Code Online (Sandbox Code Playgroud)
import Student
s1 = Student("Joe", 2, 3.0)
Run Code Online (Sandbox Code Playgroud) 输出为2 3 4 5 2293456 6 10 1355995651 12980632 0
看起来我没有正确递增
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int x[5] = {1,2,3,4,5};
vector<int> vec = {2, 4, 6, 8, 10};
for(int i : x) {
cout<<x[i]<<endl;
}
cout<<endl;
for(int i : vec) {
cout<<vec[i]<<endl;
}
}
Run Code Online (Sandbox Code Playgroud) python ×3
c++ ×2
c++11 ×2
python-3.x ×2
arrays ×1
for-loop ×1
if-statement ×1
import ×1
iterator ×1
performance ×1
pointers ×1
python-3.2 ×1