小编Par*_*dox的帖子

循环"忘记"删除一些项目

在这段代码中,我试图创建一个函数anti_vowel,它将从字符串中删除所有元音(aeiouAEIOU).我认为它应该可以正常工作,但是当我运行它时,示例文本"嘿看单词!" 以"Hy lk Words!"返回.它"忘记"删除最后一个'o'.怎么会这样?

text = "Hey look Words!"

def anti_vowel(text):

    textlist = list(text)

    for char in textlist:
        if char.lower() in 'aeiou':
            textlist.remove(char)

    return "".join(textlist)

print anti_vowel(text)
Run Code Online (Sandbox Code Playgroud)

python string list

77
推荐指数
6
解决办法
7039
查看次数

C++编译器允许循环定义?

在为树写一些代码时犯了一个错误,我遇到了以下奇怪的问题.我已经删除了很多这个例子,所以它只是一个线性树.

基本上,在main()函数中,我想将一个Node附加到我的树上,但是我没有将它附加到"tree.root",而是将它附加到"root".但是,令我惊讶的是,它不仅编译得很好,而且我能够在节点上调用方法.它只在我尝试访问"value"成员变量时出错.

我想我的主要问题是,为什么编译器没有抓到这个bug?

std::shared_ptr<Node> root = tree.AddLeaf(12, root);
Run Code Online (Sandbox Code Playgroud)

由于RHS上的"根"是一个平坦的未声明变量.另外,出于好奇,如果编译器让它们通过,那么循环定义是否具有实际用例?这是代码的其余部分:

#include <iostream>
#include <memory>

struct Node
{
    int value;
    std::shared_ptr<Node> child;

    Node(int value)
    : value {value}, child {nullptr} {}

    int SubtreeDepth()
    {
        int current_depth = 1;
        if(child != nullptr) return current_depth + child->SubtreeDepth();
        return current_depth;
    }
};

struct Tree
{
    std::shared_ptr<Node> root;

    std::shared_ptr<Node> AddLeaf(int value, std::shared_ptr<Node>& ptr)
    {
        if(ptr == nullptr)
        {
            ptr = std::move(std::make_shared<Node>(value));
            return ptr;
        }
        else
        {
            std::shared_ptr<Node> newLeaf = std::make_shared<Node>(value);
            ptr->child = std::move(newLeaf);
            return ptr->child; …
Run Code Online (Sandbox Code Playgroud)

c++ c++14

22
推荐指数
2
解决办法
2513
查看次数

C++ 在 if 语句中分配引用变量

如何根据 if 语句分配引用变量?

例如,以下示例不起作用,因为“smaller”在 if 语句之外没有范围。

int x = 1;
int y = 2;
if(x < y)
{
    int & smaller = x;
}
else if (x > y)
{
    int & smaller = y;
}
/* error: smaller undefined */
Run Code Online (Sandbox Code Playgroud)

但是,以下示例也不起作用,因为必须立即将引用分配给对象。

int x = 1;
int y = 2;
int & smaller; /* error: requires an initializer */
if(x < y)
{
    smaller = x;
}
else if (x > y)
{
    smaller = y;
}
Run Code Online (Sandbox Code Playgroud)

我可以使用三元 if 语句实现引用赋值,但如果我不能使用它怎么办?三元 …

c++ reference

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

C ++带有remove_if的异常行为

我试图用std::remove_if它从一个简单的字符串中删除空格,但结果却很奇怪。有人可以帮我弄清楚发生了什么吗?

该代码是:

#include <iostream>
#include <algorithm>
#include <string>

int main(int argc, char * argv[])
{
    std::string test = "a b";
    std::remove_if(test.begin(), test.end(), isspace);
    std::cout << "test : " << test << std::endl; 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望这只是打印出来:

test : ab
Run Code Online (Sandbox Code Playgroud)

但是我得到了

test : abb
Run Code Online (Sandbox Code Playgroud)

尝试另一个字符串,我得到:

输入:“ a bcde uv xy”

输出:“ abcdeuvxy xy”

似乎它在复制最后一个“单词”,但有时会添加一个空格。我怎样才能在不做任何奇怪的事情的情况下删除所有空间?

c++ string stl-algorithm

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

如何在C++中快速创建数组

我已经习惯使用Python或Matlab,我可以创建一个列表:

min = 5;
step = 2;
max = 16;
Range = min:step:max;
Run Code Online (Sandbox Code Playgroud)

和范围将是列表[5,7,9,11,13,15].

是否有一种等效的简单方法可以在C++中生成像"Range"这样的列表?到目前为止,我能想到的最简单的事情是使用for循环,但这是相当繁琐的.

c++ arrays matlab

5
推荐指数
2
解决办法
278
查看次数

使用列表理解从元组中提取

我有一个元组列表:

tuples = [(0,1), (2,0), (3,4), (1,2) etc. ]
Run Code Online (Sandbox Code Playgroud)

我想创建另一个列表,其中包含与0配对的所有数字.我尝试使用列表解析来执行此操作:

relations = [x[1] if x[0] == 0 else x[0] if x[1] == 0 for x in tuples]
Run Code Online (Sandbox Code Playgroud)

但是,这会产生错误.Python似乎不喜欢'x'是一个元组.可以用列表理解来定义"关系",还是需要写出更长的代码?

python list-comprehension

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

指向上一个值而不递减指针

这是一个非常简单的问题.

基本上,假设我有两个迭代器,it1并且it2.给定值it1,我想定义it2为更早地指向一个地址.如果我可以在一行中完成它会很酷,例如:

vector<int>::iterator it2 = --it1;
Run Code Online (Sandbox Code Playgroud)

但是,这会同时减少它1,所以我必须重新增加它1.

vector<int>::iterator it2 = --it1;
++it1;
Run Code Online (Sandbox Code Playgroud)

如果这两行涉及性能密集型循环,我会有很多it1来回无缘无故,只是为了定义it2.另一方面,如果我这样做:

vector<int>::iterator it2 = it1;
--it2;
Run Code Online (Sandbox Code Playgroud)

这也略微不理想,因为它涉及两个步骤.有没有办法一个一个?

c++ pointers

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

使用巨型工作空间数组用于Fortran子例程

我常常在查看线性代数子程序的遗留Fortran代码时,注意到这一点:

他们不是使用一堆独立的数组,而是将所有数组连接成一个大的工作空间数组,并使用指针来划分变量的开始位置.

它们甚至将独立的非数组变量连接成数组.这样做是否有好处,如果我想编写优化代码,我应该这样做吗?

arrays fortran

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

C++ 为什么数字限制对 uint8_t 和 int8_t 不起作用?

我最近注意到 numeric_limits::max() 和 numeric_limits::min() 似乎不适用于 uint8_t 和 int8_t。这是有原因的还是可能是错误?我在自己的电脑上使用 gcc 编译器尝试过:

#include <iostream>
#include <limits>

using namespace std;

int main()
{

    std::cout << "numeric_limits<uint8_t>::max() = " << numeric_limits<uint8_t>::max() << std::endl; 
    std::cout << "numeric_limits<int8_t>::max() = " << numeric_limits<int8_t>::max() << std::endl; 
    std::cout << "numeric_limits<int8_t>::min() = " << numeric_limits<int8_t>::min() << std::endl;

    std::cout << "numeric_limits<uint16_t>::max() = " << numeric_limits<uint16_t>::max() << std::endl; 
    std::cout << "numeric_limits<int16_t>::max() = " << numeric_limits<int16_t>::max() << std::endl; 
    std::cout << "numeric_limits<int16_t>::min() = " << numeric_limits<int16_t>::min() << std::endl; 

    std::cout << "numeric_limits<uint32_t>::max() = " << …
Run Code Online (Sandbox Code Playgroud)

c++ numeric-limits

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

在GraphViz中曲线一些边

我想在GraphViz中绘制一个奇美拉型图:https://www.ibm.com/developerworks/community/blogs/jfp/resource/BLOGS_UPLOADED_IMAGES/chimera.png

因此,有一些直边,但也有特定节点之间的弯曲边,并且节点是固定的.我已经确定了我想要有弯曲边缘的所有节点,但我不能让这些边缘正确弯曲.

graph G {
size = 5
0 [shape = circle, pin=true, pos="1,4!"]; 1 [shape = circle, pin=true, pos="2,4!"]; 2 [shape = circle, pin=true, pos="3,4!"]; 3 [shap\
e = circle, pin=true, pos="4,4!"]; 4 [shape = circle, pin=true, pos="1,3!"]; 5 [shape = circle, pin=true, pos="2,3!"]; 6 [shape = cir\
cle, pin=true, pos="3,3!"]; 7 [shape = circle, pin=true, pos="4,3!"]; 8 [shape = circle, pin=true, pos="1,2!"]; 9 [shape = circle, pi\
n=true, pos="2,2!"]; 10 [shape = circle, pin=true, pos="3,2!"]; 11 …
Run Code Online (Sandbox Code Playgroud)

graphviz

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