我正在尝试编写一个应该与包含相同类型的不同容器(std::vector、QVector)一起使用的算法:
template<class Container>
boolean findpeaks(cv::Mat &m, Container<std::pair<int, double>> &peaks) {
// do stuff
peaks.push_back(std::make_pair(1, 1.0));
return true;
}
Run Code Online (Sandbox Code Playgroud)
这一个给了我
“容器”不是模板
template<template<typename> class Container>
Run Code Online (Sandbox Code Playgroud)
我得到:
错误:没有匹配的函数可用于调用“findpeaks(cv::MatExpr, std::vector >&)”
...
注意:模板参数推导/替换失败:
错误:模板参数数量错误(2,应该是 1)
调用代码:
cv::Mat m(data, true);
std::vector<std::pair<int, double>> peaks;
QVERIFY(daf::findpeaks(m.t(), peaks));
Run Code Online (Sandbox Code Playgroud)
我也尝试过这样的事情:
template<template< template<typename, typename> typename > class Container>
Run Code Online (Sandbox Code Playgroud)
警告:ISO C++ 禁止在模板模板参数中使用 typename 键;使用 -std=c++1z 或 -std=gnu++1z [-Wpedantic]
还有一些错误...
我有以下结构:
pub struct Foo<T> {
some_value: T,
}
impl<T> Foo<T> {
pub fn new(value: T) -> Self {
Self { some_value: value }
}
}
// Implement `Default()`, assuming that the underlying stored type
// `T` also implements `Default`.
impl<T> Default for Foo<T>
where
T: Default,
{
fn default() -> Self {
Self::new(T::default())
}
}
Run Code Online (Sandbox Code Playgroud)
Foo::default()如果T实现Default,我希望可用,否则不可用。
是否可以在 Rust 中指定“条件实现”,当且仅当满足某个泛型类型特征约束时,我们才实现特征?如果不满足约束,则不会实现目标特征(Default在本例中)并且没有编译器错误。
换句话说,是否可以通过以下方式使用上面的通用结构?
fn main() {
// Okay, because `u32` implements `Default`.
let foo …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个可以在没有任何动态内存分配的情况下填充结构的通用函数。
以下代码是我正在尝试做的事情的一个简单示例。此代码不会编译为incomplete type 'void' is not assignable.
请注意,这是一个突出我的问题的玩具示例。我真的不想转换颜色;我只想强调结构在数据类型和大小上会有所不同。
#include <stdio.h>
typedef struct {
int r;
int g;
int b;
} rgb_t;
typedef struct {
float c;
float m;
float y;
float k;
} cmyk_t;
typedef enum { RGB, CMYK } color_t;
void convert_hex_to_color(long hex, color_t colorType, void* const out) {
if (colorType == RGB) {
rgb_t temp = { 0 };
// Insert some conversion math here....
temp.r = 1;
temp.g = 2;
temp.b = 3; …Run Code Online (Sandbox Code Playgroud) using 指令不适用于概念吗?为什么?
下面的示例不起作用,我收到一个编译器错误,提示它需要一个类型。
#include <concepts>
namespace A::X {
struct BaseA {};
template < typename AType >
concept DerivedFromA = std::derived_from < AType, BaseA >;
}
namespace A {
using DerivedFromA = X::DerivedFromA;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够访问概念DerivedFromA从命名空间A的DerivedFromA,而不是X::DerivedFromA
我正在尝试编写一个函数,使用整数键将Maps转换为相应的数组.我已完成基本情况,但我正在尝试编写递归情况(即多维数组:将Map [Int,Map [Int,X]]转换为Array [Array [X]]).
这个任务产生于需要从流构造数组而不知道数组预先有多大,允许元素以随机顺序离开流的可能性以及重复元素离开流的可能性.
我有一个功能:
def toArrayHard[X:ClassManifest](x:scala.collection.Map[Int, X]):Array[X] =
{
if (x.size == 0) new Array(0)
else
{
val max:Int = 1 + x.keys.max
val a:Array[X] = new Array(max)
var i = 0
while (i < max)
{
a(i) = x(i)
i += 1
}
a
}
}
Run Code Online (Sandbox Code Playgroud)
注意,我知道如果地图包含密钥k但是不包含密钥i,其中0 <= i <k,代码将失败.这对我来说没问题.
现在我希望对任意深度的多维数组做同样的事情.例如,在Map [Int,Map [Int,X]]到Array [Array [X]]之间进行转换.不幸的是,我被这些类型绊倒了.使用以上作为基础案例,这是我到目前为止所拥有的:
def toArrayHardRec[X:ClassManifest](x:scala.collection.Map[Int, X]):Array[X] =
{
import scala.collection.Map
if (x.size == 0) new Array(0)
else
{
x match
{
case t:Map[Int, Map[Int, …Run Code Online (Sandbox Code Playgroud) int fn(unsigned int x)
{
int count = 0 ;
for(; x!=0; x&=(x-1))
count ++;
return count;
}
Run Code Online (Sandbox Code Playgroud)
我在编译器中尝试过,但无法弄清楚发生了什么.我认为这与位数有关x,但是什么?
给出这样的结构:
struct Foo{
std::string name;
int value;
};
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来传递类型的实例化,
以及数据成员名称,
每个都作为单独的参数.
虽然这是不正确的语法,但我认为它有助于说明我要完成的任务:
template<typename MemberName>
void Print(Foo foo, MemberName member_name){
std::cout << foo.member_name << '\n';
}
int main(){
Foo foo{"name",100}; //create instance
Print(foo,.name); //prints name
Print(foo,.value); //prints 100
}
Run Code Online (Sandbox Code Playgroud)
如何在C++中实现?
另外,我没有权限修改类型的减速度.
c++ templates function parameter-passing generic-programming
在python中,我们可以这样做:
int_list = [1, 2, 3, 4, 5]
print(sum(int_list)) # prints 15
float_tuple = (1.2, 3.4, 9.9)
print(sum(float_tuple)) # prints 14.5
Run Code Online (Sandbox Code Playgroud)
该sum函数接受任何可迭代的元素,这些元素知道如何相互添加并且为0并生成总和.
我想在C++ 11中创建一个相同的函数.我知道存在accumulate方法,但我想要一个带有单个参数的函数.基本上,我想知道如何编译以下代码:
#include <string>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
template<typename iterable>
auto sum(iterable iterable_) {
auto it = iterable_.begin();
auto end = iterable_.end();
if (it == end) {
return 0;
}
auto res = *(it++);
while (it != end) {
res += *it++;
}
return res;
}
int main() {
std::vector<int> …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的搜索创建一个通用方法,但我不知道如何从我的类中返回字段列表.
假设我有一堂课:
public class Table
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我想返回一个如下所示的列表:
"ID"
"Name"
"Address"
Run Code Online (Sandbox Code Playgroud)
我怎么做?
试过这样的事情:
FieldInfo[] fields = typeof(T).GetFields(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
string[] names = Array.ConvertAll<FieldInfo, string>(fields,
delegate(FieldInfo field) { return field.Name; });
Run Code Online (Sandbox Code Playgroud)
但它在字段名称之后有一些不必要的文本
它不重复,因为在我的情况下GetProperties(). Select(f => f.Name)产生了不同
我正在阅读“第2部分急躁的scala”第14.4节,对上下文感到困惑:
您可以匹配表达式的类型,例如:
obj match {
case x:Int => x
case s:String => Integer.parseInt(s)
case _:BigInt => Int.MaxValue
case _ => 0
}
Run Code Online (Sandbox Code Playgroud)
与类型匹配时,必须提供变量名称。否则,您匹配对象:
obj match {
case _:BigInt => Int.MaxValue // Matches any object of type BigInt
case BigInt => -1 // Matches the BigInt object of type Class
}
Run Code Online (Sandbox Code Playgroud)
我很困惑的是,如果我进行如下测试,那么如何理解obj哪个请求是expression:
val x = 121
val obj : Any = x
obj == 121 // true
obj match {
case x:Int => x
case s:String => …Run Code Online (Sandbox Code Playgroud)