我想了解如何处理链查询.例如,让我们考虑以下查询
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
实例IEnum0
GetEnumerator()
返回IEnum1
实例的调用IEnum1
GetEnumerator()
返回IEnum2
实例的调用IEnum2
GetEnumerator()
返回IEnum3
实例的调用2.调用MoveNext(向后传递)
.Sum()
呼吁MoveNext()
对IEnum3
IEnum3
呼吁MoveNext()
对IEnum2
IEnum2
呼吁MoveNext()
对IEnum1
IEnum1
呼吁MoveNext()
对IEnum0
3.从MoveNext返回(前后传球)
IEnum0
移动到元素-1
并返回true
.IEnum1
检查是否-1 …
我正在尝试第一次实施五规则。在阅读了大量关于最佳实践的建议后,我最终得到了一个解决方案,其中复制/移动赋值运算符似乎存在一些冲突。
这是我的代码。
#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) 我是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表达式?
我需要像这样创建一个泛型类的实例:
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) 我有模板类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) 一个简单的代码
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)
看起来真的很难看.
我想从项目main
内部的方法调用项目内的方法.当我写的时候A
main
B
B
main.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)
可以打这样的电话吗?
列表枚举器在放入列表时不起作用.例如,在两个列表的情况下(两个枚举器)
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# ×4
c++ ×4
linq ×3
c++17 ×1
casting ×1
destructor ×1
generics ×1
ienumerable ×1
ienumerator ×1
iterator ×1
lambda ×1
pointers ×1
reflection ×1
templates ×1
unique-ptr ×1