小编Sal*_*osa的帖子

不能在数组上使用.begin()或.end()

错误如下:

请求'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)

c++ arrays pointers iterator c++11

25
推荐指数
2
解决办法
4万
查看次数

我应该在Python 3.x的if语句中使用'in'或'or'来检查多个值的变量吗?

假设我有以下,这是更好,更快,更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)

python performance if-statement python-3.x

5
推荐指数
1
解决办法
85
查看次数

Python 3中的类对象/实例是通过引用传递的吗?

此程序修改类"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)

python python-3.x

4
推荐指数
2
解决办法
4817
查看次数

如何导入我在Python中创建的类并创建该类的实例?我收到一个错误,说对象不可调用

Student.py

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)

Database.py

import Student 

s1 = Student("Joe", 2, 3.0)
Run Code Online (Sandbox Code Playgroud)

python import python-3.2

1
推荐指数
1
解决办法
2012
查看次数

为什么这些基于范围的循环不能正常工作?

输出为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)

c++ for-loop c++11

1
推荐指数
1
解决办法
115
查看次数