小编man*_*lio的帖子

如何将矢量与Boost.Test进行比较?

我正在使用Boost Test来测试一些C++代码.

我有一个值的向量,我需要与预期的结果进行比较,但我不想手动检查循环中的值:

BOOST_REQUIRE_EQUAL(values.size(), expected.size());

for( int i = 0; i < size; ++i )
{
    BOOST_CHECK_EQUAL(values[i], expected[i]);
}
Run Code Online (Sandbox Code Playgroud)

主要问题是循环检查不会打印索引,因此需要进行一些搜索才能找到不匹配.

我可以使用std::equalstd::mismatch在两个向量上,但这也需要很多样板.

有更清洁的方法吗?

c++ boost unit-testing boost-unit-test-framework

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

C++标准库中的boost :: optional替代品

我试图让我的程序在没有boost使用的情况下工作,但是找不到某些有用模式的替代方案.也就是说,我boost::optional在标准库中找不到-likewise模式.是否有boost::optional(C++ 11或其他地方)的标准替代方案?

c++ boost std c++11 boost-optional

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

在android表格布局中右对齐列

我想绘制一个表,其中最后一列应位于表的最右侧.

这是表行的样子:

Admin (2)New
Network (2)New
Run Code Online (Sandbox Code Playgroud)

这应该是这样的:

Admin (2)         New
Network (2)       New
Run Code Online (Sandbox Code Playgroud)

XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="45dp"
    android:gravity="center" android:background="@color/list_bg">

    <TableLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TableRow>



            <ImageView android:id="@+id/t1" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView android:id="@+id/t2" android:typeface="normal"
                android:singleLine="true" android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" />
            <TextView android:id="@+id/t10" android:typeface="normal"
                android:singleLine="true" android:text=" " android:textSize="14sp"
                android:textStyle="normal" android:layout_width="wrap_content"
                android:textColor="#000000" android:layout_height="wrap_content" />
            <TextView android:id="@+id/t4" android:typeface="normal"
                android:visibility="gone" android:singleLine="true" android:text="("
                android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" />
            <TextView android:id="@+id/t5" android:typeface="normal"
                android:visibility="gone" android:singleLine="true"
                android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" /> …
Run Code Online (Sandbox Code Playgroud)

android android-tablelayout

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

使用emplace_back避免移动构造函数调用的最佳方法是什么?

刚学会了C++ 17中保证的复制省略.根据该问题的答案:

执行return T();此操作时,将通过a初始化函数的返回值prvalue.由于该函数返回T,因此不会创建临时函数; prvalue简单的初始化直接初始化返回值.

要理解的是,由于返回值是a prvalue,它还不是一个对象.它只是一个对象的初始化器,就像T()是.

所以我想知道,这是否适用于除以下之外的任何事情:

T f() {return T();}
T t = f();
Run Code Online (Sandbox Code Playgroud)

所以我编写了这段代码emplace_back来测试它:

#include <vector>
#include <iostream>
struct BigObj{
  BigObj() = default;
  BigObj(int) { std::cout << "int ctor called" << std::endl;  }
  BigObj(const BigObj&){
    std::cout << "copy ctor called" << std::endl;
  }
  BigObj(BigObj&&){
    std::cout << "move ctor called" << std::endl;
  }
};
BigObj f(){ return BigObj(2); }
int g(){ return 2; }
int main(){ …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 emplace c++17

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

在C++中键入擦除:boost :: shared_ptr和boost :: function如何工作?

类型擦除 - 你怎么称呼它?

如何boost::shared_ptr存储其删除器以及如何boost::function存储其功能对象?

有没有教授这个技巧的教程?

使用类型擦除函数对象的运行时成本是多少?

c++ boost type-erasure

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

分布和内部状态

在Stackoverflow上,有很多关于从a-priory未知范围生成均匀分布的整数的问题.例如

典型的解决方案是这样的:

inline std::mt19937 &engine()
{
  thread_local std::mt19937 eng;
  return eng;
}

int get_int_from_range(int from, int to)
{
  std::uniform_int_distribution<int> dist(from, to);
  return dist(engine());
}
Run Code Online (Sandbox Code Playgroud)

鉴于分布应该是一个轻量级对象并且没有性能问题需要多次重新创建它,看起来即使是简单的分发也可能很好并且通常会有一些内部状态.

所以我想知道是否通过不断重置它来干扰分布如何工作(即在每次调用时重新创建分布get_int_from_range)我得到了正确分布的结果.

Pete Becker和Steve Jessop之间进行了长时间的讨论,但没有最后的说法.在另一个问题中(我应该保留随机分布对象实例还是可以随时重新创建它?)内部状态的"问题​​"似乎并不重要.

C++标准是否对此主题做出了任何保证?

以下实现(来自N4316 - std :: rand替换)是否更可靠?

int get_int_from_range(int from, int to)
{
  using distribution_type = std::uniform_int_distribution<int>;
  using param_type = typename distribution_type::param_type;

  thread_local std::uniform_int_distribution<int> dist;
  return dist(engine(), param_type(from, to));    
}
Run Code Online (Sandbox Code Playgroud)

编辑

这重用了分发的可能的内部状态,但它很复杂,我不确定它是否值得麻烦:

int get_int_from_range(int from, int to)
{
  using …
Run Code Online (Sandbox Code Playgroud)

c++ random distribution prng c++11

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

与简单的分离线程相比,为什么std :: async会变慢?

我已经多次被告知,我应该使用参数std::async来解决火灾和遗忘类型的任务std::launch::async(所以它最好在新的执行线程上使用它).

在这些陈述的鼓励下,我想看看如何std::async比较:

  • 顺序执行
  • 一个简单的分离 std::thread
  • 我简单的异步"实现"

我天真的异步实现看起来像这样:

template <typename F, typename... Args>
auto myAsync(F&& f, Args&&... args) -> std::future<decltype(f(args...))>
{
    std::packaged_task<decltype(f(args...))()> task(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
    auto future = task.get_future();

    std::thread thread(std::move(task));
    thread.detach();

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

没有看中这里,包函子fstd::packaged task与它的参数一起,启动它的一个新的std::thread被分离,并用返回std::future的任务.

现在代码测量执行时间std::chrono::high_resolution_clock:

int main(void)
{
    constexpr unsigned short TIMES = 1000;

    auto start = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < TIMES; ++i)
    {
        someTask();
    }
    auto dur = …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading asynchronous c++11 stdasync

18
推荐指数
2
解决办法
2531
查看次数

解释差异进化方法

有人可以解释差异进化方法吗?维基百科的定义非常技术性.

一个简单的例子后面的一个愚蠢的解释将是赞赏:)

evolutionary-algorithm differential-evolution

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

由<iterator>引入的sys/sysmacros.h中定义的主要和次要宏

我正在编写一个具有类似矩阵结构的类,我希望有一个名为minor的成员函数与矩阵操作相同.这会触发一些错误.我系统上的最小测试用例:

#include <iterator>
void minor(int row, int col);
Run Code Online (Sandbox Code Playgroud)

编译时,clang提供以下错误:

$ clang++ -Weverything -std=c++11 test.cpp 
test.cpp:2:21: error: too many arguments provided to function-like macro invocation
void minor(int row, int col);
                    ^
/usr/include/x86_64-linux-gnu/sys/sysmacros.h:67:10: note: macro 'minor' defined here
# define minor(dev) gnu_dev_minor (dev)
         ^
test.cpp:2:6: error: variable has incomplete type 'void'
void minor(int row, int col);
     ^
2 errors generated.
$
Run Code Online (Sandbox Code Playgroud)

sys/sysmacros.h的相关部分是:

/* Access the functions with their traditional names.  */
# define major(dev) gnu_dev_major (dev)
# define minor(dev) gnu_dev_minor (dev)
# …
Run Code Online (Sandbox Code Playgroud)

c++ macros glibc libstdc++

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

srand(时间(NULL))不好吗?

被认为有害的rand()中,它被指出srand(time(NULL))是坏的,因为srand需要一个unsigned int,但对于Microsoft的编译器,time_t默认情况下是64位数,因此发生缩小转换.但是,time_t是实现定义的.

既然我看到srand(time(NULL))如此普遍(即使在这个网站上),是否应该气馁?

c++ random srand

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