小编vcx*_*178的帖子

在基于范围的for循环中使用声明的变量

在下面的例子中,i有函数范围.但似乎我不能i在第二个for循环使用.为什么不起作用for (i : v1),但for (int i : v1)有效?

#include<iostream>
#include<string>
#include<vector>

int main()
{
    std::vector<int> v1;
    int i;
    while(std::cin>>i)
    {
        v1.push_back(i);
    }

    for(i : v1) //for (int i:v1) works
        std::cout<<i<<"\t";
    cout<<std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

51
推荐指数
2
解决办法
2073
查看次数

如何删除多行NumPy数组?

如何删除多行NumPy数组?例如,我想删除xx.

import numpy as np
x = np.random.rand(10, 5)
np.delete(x, (0:5), axis=0)
Run Code Online (Sandbox Code Playgroud)

python numpy

6
推荐指数
2
解决办法
9815
查看次数

什么是这个C++警告 - 控制到达非void函数的结尾

我正在写这个函数定义.

#include<iostream>

int abs_fun(int x);

int main()
{
    int i =abs_fun(0);
    std::cout<<i;
    return 0;
}

int abs_fun(int x)
{
    if (x<0)
        return -x;
    else if(x>=0) //warning
        return x;
}
Run Code Online (Sandbox Code Playgroud)

我正在编译代码g++ -Wall -Wconversion -Wextra p4.cpp -o p4并在结束时收到警告abs_fun()因为warning: control reaches end of non-void function [-Wreturn-type] }如果我只是写else而不是else if,那么警告就会消失.有什么建议警告出现?

c++

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

标签 统计

c++ ×2

numpy ×1

python ×1