小编use*_*083的帖子

C++ 17中新的基于范围的for循环如何帮助Ranges TS?

委员会改变了基于范围的for循环:

  • C++ 11:

    {
       auto && __range = range_expression ; 
       for (auto __begin = begin_expr, __end = end_expr; 
           __begin != __end; ++__begin) { 
           range_declaration = *__begin; 
           loop_statement 
       }
    } 
    
    Run Code Online (Sandbox Code Playgroud)
  • 到C++ 17:

    {        
        auto && __range = range_expression ; 
        auto __begin = begin_expr ;
        auto __end = end_expr ;
        for ( ; __begin != __end; ++__begin) { 
            range_declaration = *__begin; 
            loop_statement 
        } 
    }
    
    Run Code Online (Sandbox Code Playgroud)

人们说这将使Ranges TS更容易实现.你能举个例子吗?

c++ for-loop c++11 c++17

66
推荐指数
2
解决办法
8258
查看次数

在简短的帖子中嵌入html

我有一个像这样的HTML消息:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript">
        (function(){
            document.writeln("<iframe src=\"http://www.example.com" width=\"100%\" height=\"100%\" frameborder=\"0\"></iframe>");
        })();
    </script>
</head>
<body>
<h1>hello world!</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我怎么能把它作为一个消息松弛?松弛接受HTML吗?

var message = {
        mrkdwn: true,
        text: "",      //This does not accept my above HTML code
        attachments : []
    };

    slacker.notify(message, function(err, result) {
        callback(err, result);
    });
Run Code Online (Sandbox Code Playgroud)

html javascript slack

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

如何使用rangev3范围实现flatmap

我有一个非常简单的flatmap函数在C++中实现std::vector,但有人建议范围通常更好.这是基于矢量的解决方案:

// flatmap: [A] -> (A->[B]) -> [B]    
template<typename T, typename FN>
static auto flatmap(const std::vector<T> &vec, FN fn) 
     -> std::vector<typename std::remove_reference<decltype(fn(T())[0])>::type> {
    std::vector<typename std::remove_reference<decltype(fn(T())[0])>::type> result;
    for(auto x : vec) {
        auto y = fn(x);
        for( auto v : y ) {
            result.push_back(v);
        }
    }
    return result;
};
Run Code Online (Sandbox Code Playgroud)

我还建议使用迭代器,但这会破坏函数的良好可组合性:

map(filter(flatmap( V, fn), fn2), fn3)
Run Code Online (Sandbox Code Playgroud)

我认为在范围v3世界中,我的目标是将上述内容写成:

auto result = v | flatmap(fn) | filter(fn2) | transform(fn3);
Run Code Online (Sandbox Code Playgroud)

感觉flatmap应该只是一个微不足道的组合views::for_each,yield_from而且transform,我正在努力找出如何将它们连接在一起.

c++ c++11 range-v3

8
推荐指数
2
解决办法
721
查看次数

为什么通过weak_ptr调用这么慢?

我已经阅读了问题weak_ptr的性能损失是什么?但我自己的测试显示不同的结果.

我正在为聪明的指针做代表.下面的简单代码显示了重现性能问题weak_ptr.谁能告诉我为什么?

#include <chrono>
#include <functional>
#include <iostream>
#include <memory>
#include <stdint.h>
#include <string>
#include <utility>

struct Foo
{
    Foo() : counter(0) { incrStep = 1;}

    void bar()
    {
        counter += incrStep;
    }

    virtual ~Foo()
    {
        std::cout << "End " << counter << std::endl;
    }
private:
    uint64_t counter;
    uint64_t incrStep;
};

void pf(const std::string &md, const std::function<void()> &g)
{
    const auto st = std::chrono::high_resolution_clock::now();
    g();
    const auto ft = std::chrono::high_resolution_clock::now();
    const auto del = std::chrono::duration_cast<std::chrono::milliseconds>(ft - …
Run Code Online (Sandbox Code Playgroud)

c++ performance g++ c++11

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

新的C++ Mongo驱动程序:如何查看类型以及如何获取字符串值

我有两个问题,我在教程中找不到答案.

我得到一个文档,然后是doc中的一个元素,如下所示:

        bsoncxx::document::element e = doc["id"];

        if (!e || e.type() != bsoncxx::type::k_int32) return ERROR;
        int id = e.get_int32();
Run Code Online (Sandbox Code Playgroud)

有没有办法为类型获取字符串值,以进行调试?喜欢:

        std::cout << e.type() << std::endl;
Run Code Online (Sandbox Code Playgroud)

(哪个不起作用)

第二个问题是如何将utf8类型值转换为std :: string.这不起作用:

        e = doc["name"];
        if (!e || e.type() != bsoncxx::type::k_utf8) return ERROR;
        string name = e.get_utf8().value;
Run Code Online (Sandbox Code Playgroud)

有小费吗?

c++ mongodb

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

将 void 指针传递给 LLVM IRBuilder CreateCall

我正在创建一个 LLVM 函数调用。如何将 void 指针作为参数传递给该调用。我有“_testFunc”作为我的函数,需要传递 void 指针作为参数。

llvm::Function *testFunc= m_mod->getFunction("_testFunc");

llvm::IRBuilder<> builder(instruction.getNextNode());

llvm::Value *arg = argument is void pointer

builder.CreateCall(testFunc, arg);
Run Code Online (Sandbox Code Playgroud)

所以这arg必须是一个空指针。

c++ llvm

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

如何获取矩阵中元素周围邻居的所有值?

我需要在python中获取矩阵中元素周围所有邻居的值。假设我有一个像下面这样的矩阵,

  matrix=[[1,2,3,4],
             [5,6,7,8],
             [9,10,11,12]]
Run Code Online (Sandbox Code Playgroud)

对于第一个元素,即 ,matrix[0][0]邻居是[2,5,6]

对于matrix[0][1],邻居是[1,3,5,6,7]

对于matrix[0][2],邻居是[2,4,6,7,8]

对于给定的元素,我需要获取这些值列表。

我可以通过比较 i=0,j=0 时的 i,j 值来做同样的事情,得到 matrix[0][1], matrix[1][0], matrix[1][1] 使用 switch case 等等。但它会变成冗长的代码。是否有任何内置功能或任何模块可以使上述任务更简单?

python list matrix nearest-neighbor python-3.x

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

函数 split() 已弃用并爆炸 ....未定义的偏移量:1 E_NOTICE 文件中的错误

我收到此错误: function split() deprecated

list ($kk, $vv) = split( '  ', $buf, 2);
Run Code Online (Sandbox Code Playgroud)

当我将其替换为explodepreg_split 出现此错误时 Undefined offset: 1 E_NOTICE Error in file

list ($kk, $vv) = explode( "  ", $buf, 2);
Run Code Online (Sandbox Code Playgroud)

这是完整的代码

function get_toprotatingbanners()
{
    $s = array ();
    $file = fopen ('inc/adsadmin/toprotatingbanners.php', 'r');
    if ($file)
    {
        while ($buf = fgets ($file, 20000))
        {
            $buf = chop ($buf);
            if (($buf != '<?/*' AND $buf != '*/?>'))
            {
                list ($kk, $vv) = explode(" ", $buf, 2);
                $s[$kk] = …
Run Code Online (Sandbox Code Playgroud)

php split undefined deprecated offset

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

用作模板默认参数时,不调用完全专用的重载方法

我希望能够通过调用重载函数之一(具有完全专业化的模板)来指定默认的非类型模板参数。以下代码表示问题:我期望FieldType2 getDefaultField<FieldType2>()被打印,但Called getDefaultField() !被打印。

#include <iostream>

enum class FieldType1 {
    Description1,
    Description2,
    Description3
};

enum class FieldType2 {
    Description1,
    Description2,
    Description3
};

template<class FiledType>
struct FieldDescription {
    constexpr static int startPos{0};
    constexpr static FieldType fieldType{}; 
};

struct ConcreteField2 : public FieldDescription<FieldType2> {};

template<class FieldType>
constexpr FieldType getDefaultField() {
    return FieldType{};
};

template<>
constexpr FieldType1 getDefaultField<FieldType1>() {
    return FieldType1::Description1;
};

template<>
constexpr FieldType2 getDefaultField<FieldType2>() {
    return FieldType2::Description3;
};

template<class FieldDescr,
        decltype(FieldDescr::fieldType) fieldType = getDefaultField<decltype(FieldDescr::fieldType)>()>
void process() { …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++17

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

如何为数字到字符串转换选择最小的缓冲区大小?

我尝试使用最小缓冲区std::snprintf转换uint64_tstd::string并在转换后的字符串中找到大值的错误

#include <cstdio>
#include <inttypes.h>
#include <limits>
#include <string>
#include <cstdint>

// assume this is maximum string size to represent any unsigned integer
// with 1 extra byte for string terminating null character
const std::size_t MAX_UINT64_WIDTH = 
        std::numeric_limits<std::uint64_t>::digits10 + 1;

int main(int, char**)
{
    uint64_t          value  = 10244450920698242790ULL;
    const std::string svalue = "10244450920698242790";

    std::string str;
    char buf[MAX_UINT64_WIDTH];
    auto l = std::snprintf(buf, MAX_UINT64_WIDTH, "%" PRIu64, value);
    if (l > 0) {
        str.assign(buf, buf + l); …
Run Code Online (Sandbox Code Playgroud)

c++

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

不能将 CRTP 用于内部类

我正在尝试使用 CRTP 来实现多种迭代器类型的通用功能。如此处 使用带有 CRTP 的内部类所述,不可能将 CRTP 用于内部类,因此我将基本迭代器类移出容器类,并在迭代器的容器内继承它。但我还是得到了

error: invalid use of incomplete type 'class ConcreteIteratorBase<Node<int>, std::iterator_traits<Node<int>*>, std::iterator<std::forward_iterator_tag, Node<int>, long int, Node<int>*, Node<int>&> >'
Run Code Online (Sandbox Code Playgroud)

错误。

这是最小的可重现示例:

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <type_traits>
#include <utility>

template <typename T>
class NotEqFromEqMixin : public T
{
public:
  bool operator!=(T const & other) const
  {
    return !(static_cast<T const &>(*this) == other);
  }
};

template <typename Iterator, typename Traits, typename StdBase>
class IteratorHelperMixin : public NotEqFromEqMixin<Iterator>, public StdBase
{
public: …
Run Code Online (Sandbox Code Playgroud)

c++ templates crtp

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