递归算法(Recursive Algorithm)
嘿我试图创建目录中所有文件的名称(带扩展名)列表,以及所有文件,但我需要使用递归算法。 我不确定我是否在正确的轨道上! C#的新手
Hey im trying to create a list of names (with extensions) of all files in the directory, and all files within all, but i need to use a recursive algorithm. Im not sure if im on the right track !!! Quite new to C#
原文:https://stackoverflow.com/questions/15129115
最满意答案
您必须使用反向参数顺序将运算符*声明为非成员函数(外部类),并从中调用另一个
template<typename T> Vector3d<T> operator*(T& s, const Vector3d<T> &v) { return Vector3d(v.v[0] * s, v.v[1] * s, v.v[2] * s); } template<typename T> Vector3d<T> operator*(const Vector3d<T> &v, T& s) { return s * v; //call the other overload }
并且不要忘记指定模板参数:
Vector3d<T> ^^^
还有一个问题......为什么要采用
T&
不是const T&
或T
? 在当前形式中,您将阻止rvalues传递。 例如,这不会编译:Vector3d<int> v; v*3; //3 isn't an lvalue, cannot bind to a nonconst reference
You must declare your operator* as a nonmember function (outside class) with inverse argument order and call the other one from it
template<typename T> Vector3d<T> operator*(T& s, const Vector3d<T> &v) { return Vector3d(v.v[0] * s, v.v[1] * s, v.v[2] * s); } template<typename T> Vector3d<T> operator*(const Vector3d<T> &v, T& s) { return s * v; //call the other overload }
And don't forget specifying the template parameters:
Vector3d<T> ^^^
One more issue... Why take
T&
istead ofconst T&
or justT
? In current form you're preventing rvalues to be passed. For example, this wouldn't compile:Vector3d<int> v; v*3; //3 isn't an lvalue, cannot bind to a nonconst reference
相关问答
更多-
inst是指向Instruction对象的指针, instList是指令对象的列表。 因此,当您尝试instList.push_back(inst)它不起作用(它期望一个真实对象而不是指向它的指针)。 你应该有instList.push_back(*inst) 。 inst is a pointer to an Instruction object and instList is a list of Instruction objects. So when you try instList.push_bac ...
-
实现'<'运算符(Implementing the '<' operator)[2021-10-27]
你可以使用std::tie 。 class Person { public: bool valid = false; std::string name = ""; std::string id = ""; bool operator<(const Person& r) const { return std::tie(valid, name, id) < std::tie(r.valid, r.name, r.id); } } 说明: std ... -
重载运算符'='和[]以写入文件中的位置(Overloading operator '=' and [] for writing to position in the file)[2023-05-31]
通常的解决方案是让operator[]返回一个代理,类似于: class UserFile { //... char get( int index ) const { ... } void set( int index, char newValue ) { ... } class Proxy { UserFile* myOwner; int myIndex; public: Proxy( UserFile* ow ... -
您的订阅运营商应该返回一个代理对象,该对象本身会实现订阅运营商。 如果我们将调用分解为obj[2][3] = 6则会产生: obj[2] - >代理对象 obj[2][6] - >引用int 通过查看obj1你会意识到除了最后一个[]所有应该返回一个代理对象。 这通常是通过使用带有“深度”参数的模板化代理,并将其专门化为适合的深度。 在你的情况下,你的代理将有一个指向T的指针,它可能产生的第一个元素,以及维度列表(作为模板参数)。 这不是微不足道的,因为你希望看到任意数量的参数。 取决于你的目标编译器是否支 ...
-
tie生成左值引用的元组,因此不适用于临时值(除非它们是const ,因此扣除元组类型会给出const引用)。 您可以使用make_tuple创建一个对象元组,或者使用forward_as_tuple来获取左值或右值引用,具体取决于参数的值类别。 或者,您可以通过向const成员返回const引用来使您的访问器工作: A const & GetARef() const { return a; } ^^^^^ tie makes a tuple of lvalue references, and so ...
-
一种可行的方法是使用无符号整数类型的无符号移位操作: infix operator >>> : BitwiseShiftPrecedence func >>> (lhs: Int64, rhs: Int64) -> Int64 { return Int64(bitPattern: UInt64(bitPattern: lhs) >> UInt64(rhs)) } print(-7 >>> 16) //->281474976710655 (使用-7进行位数为16测试似乎不是一个很好的例子,它以1 ...
-
如何实现在实例之前可用的运算符(前置位置)(How to implement an operator usable before an instance (pre position))[2023-11-13]
您必须使用反向参数顺序将运算符*声明为非成员函数(外部类),并从中调用另一个 templateVector3d operator*(T& s, const Vector3d &v) { return Vector3d(v.v[0] * s, v.v[1] * s, v.v[2] * s); } template Vector3d operator*(const Vector3d &v, T& s) { return ... -
是否可以使用重载运算符来实现另一个运算符重载?(Is it okay to use an overloaded operator to implement another operator overload?)[2023-03-30]
你可以做到这一点。 但是,更常见的是使用operator+=而不是相反的方式实现operator+ 。 myClass & operator += (const myClass & rhs) { ... ) // Return by value. // const member function. myClass operator + (const myClass & rhs) const { myClass ret = *this; // Uses copy constructor, not a ... -
a < b当且仅当b > a - 因为@Kerrek将其简单地放在上面。 bool operator>(a, b){ if(b < a){ return true; } else{ return false; } } 编辑:或者甚至进一步简化,感谢下面的@Mokosha bool operator>(a, b){ return b < a; } a < b if and only if b > a - as @Kerrek put ...
-
您的代码中缺少两件事。 您没有正确使用OR运算符 您应该使用Equals方法来比较字符串(一个好的做法) 以下代码应该工作: private void common() { if (Headername[CI] == "Yellow") { if (objnamewritten[CI].Equals("Banana") || objnamewritten[CI].Equals("lemomn") || objnamewri ...