小编Dej*_*jan的帖子

Linq:执行链查询的顺序

我想了解如何处理链查询.例如,让我们考虑以下查询

var sumOfRoots = numbers           //IEnum0
     .Where(x => x > 0)            //IEnum1
     .Select(x => Math.Sqrt(x))    //IEnum2
     .Select(x => Math.Exp(x))     //IEnum3
     .Sum();
Run Code Online (Sandbox Code Playgroud)

例如 numbers={-1, 4, 9 }.

这是幕后发生的事情:

1.获得所有普查员(正面通行证)

  • numbers调用GetEnumerator()返回(让我们用它来表示)IEnum0实例
  • IEnum0GetEnumerator()返回IEnum1实例的调用
  • IEnum1GetEnumerator()返回IEnum2实例的调用
  • IEnum2GetEnumerator()返回IEnum3实例的调用

2.调用MoveNext(向后传递)

  • .Sum()呼吁MoveNext()IEnum3
  • IEnum3呼吁MoveNext()IEnum2
  • IEnum2呼吁MoveNext()IEnum1
  • IEnum1呼吁MoveNext()IEnum0

3.从MoveNext返回(前后传球)

  • IEnum0移动到元素-1并返回true.
  • IEnum1检查是否-1 …

c# linq ienumerable ienumerator

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

C++:运算符= 在实现移动赋值时不明确

我正在尝试第一次实施五规则。在阅读了大量关于最佳实践的建议后,我最终得到了一个解决方案,其中复制/移动赋值运算符似乎存在一些冲突。

这是我的代码。

#include <vector>   
#include <memory>

template<class T> class DirectedGraph {
public:
    std::vector<T> nodes;
    DirectedGraph() {}
    DirectedGraph(std::size_t n) : nodes(n, T()) {}
    // ... Additional methods ....
};

template<class T>
DirectedGraph<T> Clone(DirectedGraph<T> graph) {
    auto clone = DirectedGraph<T>();
    clone.nodes = graph.nodes;
    return clone;
}

template<class T> class UndirectedGraph
{
    using TDirectedG = DirectedGraph<T>;
    using TUndirectedG = UndirectedGraph<T>;

    std::size_t numberOfEdges;
    std::unique_ptr<TDirectedG> directedGraph;
public:
    UndirectedGraph(std::size_t n)
        : directedGraph(std::make_unique<TDirectedG>(n))
        , numberOfEdges(0) {}

    UndirectedGraph(TUndirectedG&& other) {
        this->numberOfEdges = other.numberOfEdges;
        this->directedGraph = std::move(other.directedGraph);
    }

    UndirectedGraph(const …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading move-semantics c++17

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

对列表c#中的特定索引应用lambda表达式

我是lambda表达式的新手,我有问题将包含循环内列表索引的代码部分转换为等效的lambda表达式.

示例1:在一个列表中使用不同的索引

List<double> newList = new List<double>();
for (int i = 0; i < list.Count - 1; ++i)
{
     newList.Add(list[i] / list[i + 1]);
}
Run Code Online (Sandbox Code Playgroud)

示例2:使用两个列表中的索引

double result = 0;
for (int i = 0; i < list1.Count; ++i)
{
    result += f(list1[i]) * g(list2[i]);
}
Run Code Online (Sandbox Code Playgroud)

如何编写等效的lambda表达式?

c# linq lambda

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

使用动态泛型类型参数创建泛型类的实例

我需要像这样创建一个泛型类的实例:

 Type T = Type.GetType(className).GetMethod(functionName).ReturnType;
 var comparer = new MyComparer<T>(); // ERROR: "The type or namespace name 'T' could not be found"
Run Code Online (Sandbox Code Playgroud)

我找到了这个答案,只有通过反射才能做到这一点。但是使用反射我得到了我需要转换为我的泛型类型的对象。我试过这样

 Type myGeneric = typeof(MyComparer<>);
 Type constructedClass = myGeneric.MakeGenericType();
 object created = Activator.CreateInstance(constructedClass);
 var comparer = (T)Convert.ChangeType(created, T);// ERROR: "The type or namespace name 'T' could not be found"
Run Code Online (Sandbox Code Playgroud)

但得到同样的错误。如何解决?

这是一个完整的例子:

    public static bool Test(string className, string functionName, object[] parameters, object correctResult)
    {
        var method = Type.GetType(className).GetMethod(functionName);
        Type T = method.ReturnType;
        var myResult = method.Invoke(null, parameters); …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection casting

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

模板类c中的析构函数:如何删除可能是指针或非指针的字段?

我有模板类Array,其中模板类型T应该是指针或非指针类型.

template<class T>
class TArray
{   
     static const int max_len=100;
     T* data;
     int length;

  public:
     TArray(){data=new T[max_len]; this->length=0;} 
     void Add(T value){data[length++]=value;}   
     ~TArray();
};
Run Code Online (Sandbox Code Playgroud)

问题是如何释放空间,因为我们不能调用delete这样的指针类型

template<class T>
TArray<T>::~TArray()
{
    //for(int i =0; i < length; i++)
    //  delete data[i]; // NOT WORKING WITH OBJECTS THAT ARE NOT POINTERS !!!
    delete[] data;
}
Run Code Online (Sandbox Code Playgroud)

我们有加班

class A
{
    int* a;
  public:
    A(int n){a = new int[n];}
    ~A(){delete[] a;}
};
Run Code Online (Sandbox Code Playgroud)

并创建两个模板类实例

// Create array of doubles
TArray<double>* double_array …
Run Code Online (Sandbox Code Playgroud)

c++ templates destructor

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

将unique_ptr <Derived>转换为unique_ptr <Base>

一个简单的代码

class Base {};
class Derived : Base {};

unique_ptr<Base> Create() {
    unique_ptr<Base> basePtr = make_unique<Derived>(); // compile error
    return basePtr;
}
Run Code Online (Sandbox Code Playgroud)

产生编译错误("没有合适的转换").我找到了类似的问题,解决方案的用途std::move.我试过这个

unique_ptr<Derived> derived = make_unique<Derived>();
unique_ptr<Base> basePtr = std::move(derived); // compile error
Run Code Online (Sandbox Code Playgroud)

但现在std::move产生编译错误.我还发现问题在哪里(如果我理解的话),如果我们使用,演员应该是自动的

unique_ptr<Base> basePtr = make_unique<Derived>(new Derived()); //compile error
Run Code Online (Sandbox Code Playgroud)

但这也不起作用(编译错误),也不建议使用new智能指针.

什么是正确的解决方案?

到目前为止我找到的唯一可行解决方案

unique_ptr<Base> basePtr = unique_ptr<Base>((Base*)new Derived());
Run Code Online (Sandbox Code Playgroud)

看起来真的很难看.

c++ pointers unique-ptr

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

在c ++中从其他项目的main方法调用main方法

我想从项目main内部的方法调用项目内的方法.当我写的时候AmainBBmain.cpp

    #include "pathToProjectA/main.cpp"
Run Code Online (Sandbox Code Playgroud)

我明白了

    error C2084: function 'int main(int,char *[])' already has a body
Run Code Online (Sandbox Code Playgroud)

可以打这样的电话吗?

c++ program-entry-point

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

迭代迭代器列表linq

列表枚举器在放入列表时不起作用.例如,在两个列表的情况下(两个枚举器)

public void test()
{
    var firstList = new List<int>() { 1, 2, 3 };
    var secondList = new List<int>() { 4, 5, 6 };
    var lists = new List<List<int>>();
    lists.Add(firstList);
    lists.Add(secondList);

    // Not working
    var iterators = lists.Select(x => x.GetEnumerator()).ToList();
    iterators.ForEach(x => x.MoveNext());
    iterators.ForEach(x => Console.WriteLine(x.Current));

    // Working
    var firstIterator = iterators[0]; 
    var secondIterator = iterators[1];
    firstIterator.MoveNext(); secondIterator.MoveNext();
    Console.WriteLine(firstIterator.Current);
    Console.WriteLine(secondIterator.Current);
}
Run Code Online (Sandbox Code Playgroud)

第一部分不工作,它打印0 0,而第二部分工作,它打印1 4.

我不明白第一部分的错误是什么,以及如何解决.

c# linq iterator

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