小编eri*_*rip的帖子

Vector.erase(Iterator)导致错误的内存访问

我正在尝试进行videoObjects存储在a中的Z-Index重新排序vector.计划是确定videoObject哪个将被放置在第一个位置vector,擦除它然后将其插入第一个位置.不幸的是,该erase()功能总是导致错误的内存访

这是我的代码:

testApp.h:

vector<videoObject> videoObjects;
vector<videoObject>::iterator itVid;
Run Code Online (Sandbox Code Playgroud)

testApp.cpp:

// Get the videoObject which relates to the user event
for(itVid = videoObjects.begin(); itVid != videoObjects.end(); ++itVid) {
  if(videoObjects.at(itVid - videoObjects.begin()).isInside(ofPoint(tcur.getX(), tcur.getY()))) {
   videoObjects.erase(itVid);
  }
}
Run Code Online (Sandbox Code Playgroud)

这应该是如此简单,但我只是没有看到我在哪里走错了路.

c++ vector erase openframeworks

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

在Python中拆分分号分隔的字符串

我想分割一个以分号分隔的字符串,以便我可以使用Python存储每个单独的字符串,以用作XML标记之间的文本.字符串值如下所示:

08-26-2009;08-27-2009;08-29-2009
Run Code Online (Sandbox Code Playgroud)

它们只是存储为字符串值的日期

我想迭代遍历每个值,存储到变量并在最后将变量调用到以下代码中:

for element in iter:
    # Look for a tag called "Timeinfo"
    if element.tag == "timeinfo":
        tree = root.find(".//timeinfo")
        # Clear all tags below "timeinfo"
        tree.clear()
        element.append(ET.Element("mdattim"))
        child1 = ET.SubElement(tree, "sngdate")
        child2 = ET.SubElement(child1, "caldate1")
        child3 = ET.SubElement(child1, "caldate2")
        child4 = ET.SubElement(child1, "caldate3")
        child2.text = FIRST DATE VARIABLE GOES HERE
        child2.text = SECOND DATE VARIABLE GOES HERE
        child2.text = THIRD DATE VARIABLE GOES HERE
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

python string split

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

试图返回用`NULL`构造的`std :: unique_ptr`

我正在设计一个工厂,它创建不同类型的Foo,我正在尝试使用智能指针.

大多数似乎运行良好,但nullptr由于编译器的限制,我缺少一些重要的功能(即).

我有这个方法:

std::unique_ptr<Foo> createFoo(const std::string &fooType) {
    auto it = _registeredFoo.find(fooType); // _registeredFoo is a map
    if(it != _registeredFoo.end())
       return std::unique_ptr<Foo>(it->second());
    return std::unique_ptr<Foo>(NULL);
}
Run Code Online (Sandbox Code Playgroud)

当我测试这个方法时,它永远不会返回一个类似NULL的指针.

这就是我测试方法的方法.

std::unique_ptr<Foo> _foo = FooFactory::getInstance()->createFoo("FOO"); //FOO is registered
if(_foo) {
  std::cout << "Hello, World!" << std::endl;
} else {
  std::cout << "Goodbye, World!" << std::endl;
}

std::unique_ptr<Foo> _bar = FooFactory::getInstance()->createFoo("BAR"); // BAR is unregisered
if(_bar) {
  std::cout << "Hello, World!" << std::endl;
} else {
  std::cout << "Goodbye, World!" << …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

4
推荐指数
3
解决办法
2029
查看次数

在ubuntu中找不到pthread_create

我正在尝试在Ubuntu上运行cocos2d-x。它显示此错误:

-- Looking for IceConnectionNumber in ICE
-- Looking for IceConnectionNumber in ICE - found
-- Found X11: /usr/lib/x86_64-linux-gnu/libX11.so
-- Found OpenGL: /usr/lib/x86_64-linux-gnu/libGL.so  
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: …
Run Code Online (Sandbox Code Playgroud)

c++ ubuntu install cocos2d-iphone

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

如何解压空的variadic模板列表

我读了这个问题并认为它很有趣,所以我开始玩一些代码来看看我是否可以让它工作,但我遇到了一个问题.

我的方法是使用熟悉函数式编程的头尾习语.但是,我找不到处理空的可变参数模板列表的方法,这将是基本情况.

这是我的代码:

#include <iostream>
#include <type_traits>

class A {};
class B : public A {};
class C {};
class D : public C {};

/*

// Forward declaration
template <typename T1, typename T2, typename... Args>
struct are_convertible;

*/

// There are no Args
template <>
struct are_convertible<> {
    static const bool value = true;
};

// Check if the first two elements are convertible then recurse on the rest
template <typename T1, typename T2, typename... Args>
struct …
Run Code Online (Sandbox Code Playgroud)

c++ metaprogramming variadic-templates c++11 c++14

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

可以在Spark批处理上创建模型并在Spark流中使用它吗?

我可以在Spark批处理中创建模型并将其用于Spark流式传输以进行实时处理吗?

我已经看到Apache Spark网站上的各种示例,其中训练和预测都建立在相同类型的处理(线性回归)上.

machine-learning apache-spark spark-streaming

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

为什么单引号时 PyYAML 和 ruamel.yaml 会转义特殊字符?

我有一个 YAML 文件,想限制某个字段不包含空格。

这是一个演示我尝试的脚本:

测试文件

#!/usr/bin/env python3

import os
from ruamel import yaml

def read_conf(path_to_config):
    if os.path.exists(path_to_config):
        conf = open(path_to_config).read()
        return yaml.load(conf)
    return None

if __name__ == "__main__":
    settings = read_conf("hello.yaml")
    print("type of name: {0}, repr of name: {1}".format(type(
             settings['foo']['name']), repr(settings['foo']['name'])))
    if any(c.isspace() for c in settings['foo']['name']):
        raise Exception("No whitespace allowed in name!")
Run Code Online (Sandbox Code Playgroud)

这是我对 YAML 文件的第一次剪辑:

你好.yaml

foo:
    name: "hello\t"
Run Code Online (Sandbox Code Playgroud)

在上面的 YAML 文件中,正确引发了异常:

type of name: <class 'str'>, repr of name: 'hello\t'
Traceback (most recent call last):
  File "./test.py", line …
Run Code Online (Sandbox Code Playgroud)

yaml escaping pyyaml python-3.x ruamel.yaml

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

为什么我的 Flask 请求对象总是空的?

我正在创建一个愚蠢的端点,它只记录发布的数据。

@app.route("/add_foo", methods=['POST'])
def add_foo():
    logger.debug(request.data)
    print(request.args.get('foo'))
    return "Success"
Run Code Online (Sandbox Code Playgroud)

然后我尝试将数据发布到此端点:

>>> import requests
>>> r = requests.post("http://127.0.0.1:8080/add_foo", data={'foo': 'foobar'})
Run Code Online (Sandbox Code Playgroud)

我看到以下输出:

 * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
2016-07-28 08:59:22,432 - __main__ - DEBUG - b''
None
127.0.0.1 - - [28/Jul/2016 08:59:22] "POST /add_foo HTTP/1.1" 200 -
Run Code Online (Sandbox Code Playgroud)

有趣的是,当我通过 curl 发布数据时,我看到了同样的事情。这是我的 curl 命令:

curl -XPOST -d "{'foo': 'foobar'}" "http://localhost:8080/add_foo"
Run Code Online (Sandbox Code Playgroud)

发布的数据有什么问题?

post flask python-requests

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

元组列表列表,按第一个元素分组并添加第二个元素

假设我有以下元组列表列表:

tuples = [
             [ 
                 ('2017-04-11', '2000000.00'), 
                 ('2017-04-12', '1000000.00'), 
                 ('2017-04-13', '3000000.00')
             ],
             [
                 ('2017-04-12', '472943.00'), 
                 ('2017-04-13', '1000000.00')
             ]
             # ...
         ]
Run Code Online (Sandbox Code Playgroud)

我将如何根据第一个元素(日期)对它们进行分组并添加另一个元素。

例如,我想要这样的东西:

tuples = [('2017-04-11', '2000000.00'), ('2017-04-12', '1472943.00'), ('2017-04-13', '4000000.00')],
Run Code Online (Sandbox Code Playgroud)

python tuples list

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

使用*this作为返回值

来自Sutter&Alexandrescu的编码标准的一个例子引起了我的注意"示例2 :( std::string::append后置条件错误).当将字符附加到字符串时,如果现有的缓冲区已满,则无法分配新缓冲区会阻止操作执行其记录的功能并实现它记录的后期条件,因此是一个错误."

我无法想象在任何正常条件下,人们会检查追加的返回值,但很奇怪这个值是什么,结果证明是*this(它只能在非静态成员函数中使用).我想可以检查追加前后的字符串长度,但是如何才能访问*this

例如:

std::string::s = "Hello World";
for (int i=0; i<many millions; ++i) {
  s.append(s);
}
s.append(".");  //  Which we assume fails due to a buffer error.
Run Code Online (Sandbox Code Playgroud)

c++

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