小编lrl*_*eon的帖子

这个Sedgewick代码是否正确?

我正在解决一个优化问题,其中,我必须最大化流网络.我实现了一个基于c ++代码的流量最大化算法,该算法基于以下java代码,该代码出现在Sedgewick"Java中的算法,第三版,第5部分:图算法"一书中,它使用基于顶点的PREFLOW最大化网络流量 -推算法:

class NetworkMaxFlow
{ private Network G; private int s, t;
  private int[] h, wt;
  private void initheights()
  NetworkMaxFlow(Network G, int s, int t)
  { this.G = G; this.s = s; this.t = t;
    wt = new int[G.V()]; h = new int[G.V()];
    initheights();
    intGQ gQ = new intGQ(G.V());
    gQ.put(s); wt[t] = -(wt[s] = Edge.M*G.V());
    while (!gQ.empty())
    { int v = gQ.get();
      AdjList A = G.getAdjList(v);
      for (Edge e = A.beg(); !A.end(); e = A.nxt()) …
Run Code Online (Sandbox Code Playgroud)

c++ network-flow

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

在C++ 11中确定泛型返回类型时出错

在C++ 14应用程序的上下文中,我使用了一个可以恢复如下的方案(最小可重复性测试):

template <class Container>
struct LocateFunctions {    
  auto get_it() const // <-- here is the problem
  {
    auto ret = typename Container::Iterator();
    return ret;
  }
};

template <typename T>
struct A : public LocateFunctions<A<T>> {    
  struct Iterator {};
};

int main() {  
  A<int> a;
}
Run Code Online (Sandbox Code Playgroud)

这种方法在C++ 14中使用GCC和Clang编译器进行编译和运行.

现在我想将我的应用程序迁移到Windows,为此我正在使用MinGW.不幸的是,它的最新版本带来了GCC 4.9,它不能编译C++ 14.这似乎不是一个严重的问题,因为我可以在C++ 11中重写C++ 14结构.所以,我重写get_it()方法如下:

typename Container::Iterator get_it() const
{ 
  auto ret = typename Container::Iterator();
  return ret;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是它没有编译.两个编译器都会产生以下错误:

error: no type named ‘Iterator’ in ‘struct A<int>’
   typename Container::Iterator get_it() …
Run Code Online (Sandbox Code Playgroud)

c++ templates name-lookup c++11 c++14

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

模板方法和默认模板参数

我的问题可以通过以下代码恢复:

template <typename T> struct C2;

template <typename T> 
struct C1
{
  template <typename Type,
        template <typename Ti> class Container = C2>
  void m() {}
};


template <typename T> 
struct C2
{
  template <typename Type = int,
        template <typename Ti> class Container = C2> // <-- Here is the problem!
  void m() {}

};
Run Code Online (Sandbox Code Playgroud)

gnu编译器版本4.8.1失败,并显示以下消息:

test-temp.C:16:47: error: invalid use of type ‘C2<T>’ as a default value for a template template-parameter
      template <typename Ti> class Container = C2> 
Run Code Online (Sandbox Code Playgroud)

它指的是方法C2 :: m的默认模板参数C2. …

c++ templates scope-resolution default-arguments c++11

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

如何有效地使用Modulo?

我做了一个(对我来说)非常复杂的任务,我必须在给定n个段时计算最大可能的序列数.

我发现加泰罗尼亚数字表示这个序列,我让它工作n <= 32.我得到的结果应该计算为1.000.000.007.我遇到的问题是"q"和"p"对于一个很长的int而言变得很大而且我在分割"q"和"p"之前不能只修改1.000.000.007因为我会得到不同的结果.

我的问题是,是否有一种非常有效的方法来解决我的问题,还是我必须考虑以不同的方式存储值?我的限制如下: - 仅限stdio.h/iostream - 仅整数 - n <= 20.000.000 - n> = 2

#include <stdio.h>

long long cat(long long  l, long long  m, long long  n);

int main(){
    long long  n = 0;
    long long  val;
    scanf("%lld", &n);

    val = cat(1, 1, n / 2);

    printf("%lld", (val));

    return 0;
}

long long  cat(long long  q, long long  p, long long  n){
    if (n == 0) {
        return (q / p) % 1000000007;
    }
    else {
        q …
Run Code Online (Sandbox Code Playgroud)

c performance store division modulo

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

在qtcreator中编译C++ 14

我有一个包含部分的qt项目C++14.

最近,我改变了我的ubuntu发行版.现在我有16.04 LTS,我安装了Qt creator 4.02(建于Jun 13).

为了启用C++14编译,我输入了项目文件:

QMAKE_CXXFLAGS += -std=c++14
Run Code Online (Sandbox Code Playgroud)

但是,在构建项目时,IDE会生成以下命令:

g++ -c -pipe -std=c++14 -g -O0 -g -std=gnu++11 -Wall -W -D_REENTRANT ...
Run Code Online (Sandbox Code Playgroud)

如图所示,生成makefile的标志-std=gnu++11会覆盖标志C++14.我之前的发行版(LTS 12.04,同样的qt创建者版本)没有发生这种情况.

我试过了

CONFIG += -std=c++14
Run Code Online (Sandbox Code Playgroud)

但行为是一样的.

有人能给出任何线索吗?

c++ qt qt-creator c++14

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

使用任意数量的参数生成对lambdas的调用

事实证明,以下定义对我非常有用:

template<class Func, class... Args>
void apply_on_each_args(Func f, Args... args)
{
    (f(args), ...);
}
Run Code Online (Sandbox Code Playgroud)

基本上,在逗号运算符上折叠的参数包允许定义对带参数的函数的多个调用.例如:

apply_on_each_args([] (auto x) { cout << x << endl; }, 1, 2, "hello");
Run Code Online (Sandbox Code Playgroud)

将打电话给匿名lambda 1,2"hello".

提出这个想法,我想做同样的事情,但传递lambdas采取两个,三个等参数.例如,类似的东西

apply_on_each_args([] (auto x, auto y) { /* use x and y */ }, 1, 2, "hello",  "bye");
Run Code Online (Sandbox Code Playgroud)

任何能够实现它的线索,技术,想法等?

template-meta-programming variadic-templates generic-lambda fold-expression c++17

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

如何元编程通用列表提取以构建函数调用

我有一系列类,其方法具有以下签名:

double compute(list<T> pars)
Run Code Online (Sandbox Code Playgroud)

此方法使用通过接收的参数执行计算pars.对于每种compute(list)方法,我有另一种方法,compute(x1, x2, ..., xn)即实现实际计算的方法.因此,compute(pars)应该做一些如:

double compute(list<T> pars)
{
  T x1 = list.pop_back();
  T x2 = list.pop_back();
  // .. so on until last parameter xn
  T xn = list.pop_back();

  return compute(x1, x2, .., xn); // here the real implementation is called
}
Run Code Online (Sandbox Code Playgroud)

这种模式重复多次,唯一可以改变的是pars列表的大小,当然还有实现compute(x1, x1, ..).

我想找到一种方法来"碾压"这个重复的过程; 具体来说,提取pars列表中的参数并构建调用compute(x1, x2, .., xn).我一直在尝试做一些宏观技巧而没有成功.

我的问题是,它是否存在某种基于元编程的方式,它允许我实现compute(list<T> pars)一次并简单地重用它以便执行调用compute(x1, x2, ..., xn)

编辑:这是另一个的签名compute(x1, …

c++ template-meta-programming c++11

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

解压缩元组列表

有关如何实现此功能的任何想法?

template <class ... Ts> 
auto unzip(const list<tuple<Ts...>> & l)
{
  ...
}
Run Code Online (Sandbox Code Playgroud)

此函数将接收元组列表并返回列表元组.第一个列表将包含元素get<0>(t),等等.

我可以遍历元组的项目,当然也可以遍历列表.但我不知道如何申报一些这样的tuple<list<T1>, list<T2> ...>

任何线索或参考?

c++ template-meta-programming c++11

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

gcc 4.8.1:将c代码与c ++ 11代码相结合

我没有花太多精力去发现原因,但是gcc 4.8.1给我编译很多麻烦来编译c和c ++以及c ++ 11中的一些新东西的旧源代码.

我设法隔离了这段代码中的问题:

# include <argp.h>
# include <algorithm>
Run Code Online (Sandbox Code Playgroud)

它与g++ -std=c++0x -c -o test-temp.o test-temp.C版本4.6.3,ubuntu 12.04 编译良好

相比之下,对于4.8.1版本,相同的命令行会引发很多错误:

In file included from /home/lrleon/GCC/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/include/x86intrin.h:30:0,
                 from /home/lrleon/GCC/include/c++/4.8.1/bits/opt_random.h:33,
                 from /home/lrleon/GCC/include/c++/4.8.1/random:51,
                 from /home/lrleon/GCC/include/c++/4.8.1/bits/stl_algo.h:65,
                 from /home/lrleon/GCC/include/c++/4.8.1/algorithm:62,
                 from test-temp.C:4:
/home/lrleon/GCC/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/include/mmintrin.h: In function ‘__m64 _mm_cvtsi32_si64(int)’:
/home/lrleon/GCC/lib/gcc/x86_64-unknown-linux-gnu/4.8.1/include/mmintrin.h:61:54: error: can’t convert between vector values of different size
   return (__m64) __builtin_ia32_vec_init_v2si (__i, 0);
                                                      ^
Run Code Online (Sandbox Code Playgroud)

... 以及更多.

如果我执行,也会发生同样的事

g++ -std=c++11 -c -o test-temp.o test-temp.C; 再次,版本4.8.1

但是,如果我交换标题行,那就是

# include <algorithm>
# include <argp.h>
Run Code Online (Sandbox Code Playgroud)

然后所有编译都很好.

有人启发我了解发生了什么?

c gcc c++11

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

如何测试是否存在模板函数专门化

我正在管理单位转换.告诉我们,我达到了实现这一目标的状态.

我在不同单元之间转换的核心在于以下通用模板函数:

template <class SrcUnit, class TgtUnit> extern
double convert(double val);
Run Code Online (Sandbox Code Playgroud)

该函数的目标是将以类型SrcUnit为单位表示的物理量值转换为以类型为单位表示的另一物理量值TgtUnit.

我有一个叫做Quantity<Unit>管理值及其统一的类,这个类试图给出类型安全和自动转换.例如,我导出以下构造函数:

  template <class SrcUnit>
  Quantity(const Quantity<SrcUnit> & q)
    : unit(UnitName::get_instance())
  {
    check_physical_units(q); // verify that the physical magnitudes are the same
    value = convert<SrcUnit, UnitName>(q.value); // <<- here the conversion is done
    check_value(); // check if the value is between the allowed interval
  }
Run Code Online (Sandbox Code Playgroud)

我导出转换完成的其他东西.

因此,当有人希望管理新单元时,她会指定一个新的Unit派生类.我通过在宏中放入新类的所有必需规范来实现这一点.现在,该用户有责任编写转换函数.那就是编写convert()模板的两个特化.例如,假设您有一个名为'Kilometer and you wish to specify a new unit calledMile` 的单位.在这种情况下,您执行此操作:

Declare_Unit(Mile, …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-meta-programming c++11 c++14

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