我有问题List<T>.Reverse()和Reverse(this IEnumerable<TSource> source).看看代码:
// Part 1
List<int> list = new List<int> { 1, 2, 3 };
foreach (int x in list)
Console.Write(x);
Console.WriteLine();
list.Reverse();
foreach (int x in list)
Console.Write(x);
Console.WriteLine();
list.Reverse();
// Part2
IList<int> ilist = list;
foreach (int x in list)
Console.Write(x);
Console.WriteLine();
ilist.Reverse();
foreach (int x in ilist)
Console.Write(x);
Console.WriteLine();
ilist.Reverse();
Run Code Online (Sandbox Code Playgroud)
我的结果:
123
321
123
123
Run Code Online (Sandbox Code Playgroud)
因为-Part1 Reverse()是List<T>.Reverse(),Reverse()-Part2是Reverse(this IEnumerable<TSource> source)
我要List<int>.Reverse()在Part2中执行的IList<int>.我怎么能这样做?
考虑以下语法:
S ? A | B
A ? xy
B ? xyz
Run Code Online (Sandbox Code Playgroud)
这是我认为LR(0)解析器在给定输入时会做的事情xyz:
| xyz ? shift
x | yz ? shift
xy | z ? reduce
A | z ? shift
Az | ? fail
Run Code Online (Sandbox Code Playgroud)
如果我的假设是正确的,我们将规则B改为:
B ? Az
Run Code Online (Sandbox Code Playgroud)
现在语法突然被LR(0)解析器接受了.我认为这个新语法描述了与本问题中第一个语法完全相同的字符串集.
进一步澄清:
我想向解析器描述一种语言,而语法结构不起作用.我想获得一组字符串的最小/基本描述.对于LR(k)语法,我想最小化k.
我不熟悉模板.我刚开始学习它.为什么我在以下程序中遇到错误?
#include <iostream>
#include <string>
using std::cout;
using std::string;
template<class C>
C min(C a,C b) {
return a<b?a:b;
}
int main()
{
string a="first string";
string b="second string";
cout<<"minimum string is: "<<min(a,b)<<'\n';
int c=3,d=5;
cout<<"minimum number is: "<<min(c,d)<<'\n';
double e{3.3},f{6.6};
cout<<"minimum number is: "<<min(e,f)<<'\n';
char g{'a'},h{'b'};
cout<<"minimum number is: "<<min(g,h)<<'\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
13 [Error] call of overloaded 'min(std::string&, std::string&)' is ambiguous
6 [Note] C min(C, C) [with C = std::basic_string<char>]
Run Code Online (Sandbox Code Playgroud)
请帮我.
我查看了一些相关的堆栈溢出线程,例如模板函数重载的这种情况,这让我无法理解
和
但似乎都没有给我我正在寻找的答案,至少不是一种容易让我解释的方式.
我的问题归结为:从设计和技术的角度来看,为什么这样做是合法的:
#include <iostream>
using namespace std;
template<class T>
void func(){
cout << "Compiler inferred from void return value!\n";
}
template<class T>
int func(){
cout << "Compiler inferred from int return value!\n";
return 0;
}
int main(){
void (*thisFunc)()=func<int>;
int (*thatFunc)()=func<int>;
thisFunc();
thatFunc();
}
Run Code Online (Sandbox Code Playgroud)
但不是这个:
#include <iostream>
using namespace std;
void func(){
cout << "You won't see this because it won't compile!\n";
}
int func(){
cout << "Nor this one!\n";
return 0;
}
int main(){
void …Run Code Online (Sandbox Code Playgroud) 是否可以在D中统一实现amb运算符?
http://www.haskell.org/haskellwiki/Amb
http://www.randomhacks.net/articles/2005/10/11/amb-operator
该排序的我想的事情是:
amb([1, 2]) * amb([3, 4, 5]) == amb([3, 4, 5, 6, 8, 10])
amb(["hello", "world"]) ~ amb(["qwerty"]) == amb(["helloqwerty", "worldqwerty"])
amb(["hello", "world"]) ~ "qwerty" == amb(["helloqwerty", "worldqwerty"])
amb(["hello", "very long string"]).length = amb([5, 16])
Run Code Online (Sandbox Code Playgroud)
在最后两个例子中,确实需要将〜和.length"提升"到amb'上下文'(monad?).在前两个示例中,运算符应该只应用于amb的内容.
我已经给了它一个简短的尝试,但是当我试图解除包装类型的运算符/方法/属性(在这个例子中是*,〜和.length)时我遇到了问题.怎么在D?
谢谢,
克里斯.
我正在使用Oracle 10g(XE 10.2.0.1.0),并找到一个我不理解的行为:
select *
from employees manager
join employees worker on MANAGER.EMPLOYEE_ID = WORKER.MANAGER_ID
join departments on DEPARTMENTS.manager_id = 108
where
department_id = 100
;
Run Code Online (Sandbox Code Playgroud)
问题是我认为Oracle应该抱怨department_idwhere子句中的歧义,因为它是表employees和表中的一列departments.事实是在Oracle 10g中,它没有,结果表明它将其解释department_id为departments.但是,如果我注释掉第二个连接语句(上面的第4行),Oracle会按预期抱怨"ORA-00918:列模糊定义".
那么,有人可以帮助解释如何在Oracle 10g中定义歧义吗?或许这是一个10g的错误?
BTW:这些表是在Oracle 10g中捆绑的默认HR模式中定义的.
更新:刚刚发现了一个相关的帖子: 为什么Oracle SQL神秘地解决了一个连接中的歧义而在其他连接中没有
在这个答案的背景下,问题就出现了.
考虑一个例子:
struct foo {
int value;
operator int&(){ return value; }
operator int(){ return value; }
};
int main () {
int &a(foo{}); // #1
//int &b{foo{}}; // #2 -- ambiguity
int &c = foo{}; // #3
//int &d = {foo{}}; // #4-- ambiguity
int &d { a }; // #5
int &e = { a }; // #6
(void)a;
(void)c;
(void)d;
(void)e;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么#2和#4引起歧义,而#1和#3则不然.所以问题是 - 为什么直接列表初始化会导致隐式强制转换引用的歧义,如果声明类型和对类型的引用的转换运算符?
通过不成为隐式转换恶魔,这个问题很容易解决,但对我来说这似乎是一个奇怪的不一致。这是一个例子:
\n#include <unordered_set>\n\nvoid test1(int a, int b); // Overloaded function with two different\nvoid test1(std::unordered_set<char> set); // possible argument types\n\nstruct Test2 {\n Test2(int a, int b); // Overloaded constructor with two different\n Test2(std::unordered_set<char> set); // possible argument types\n};\n\nint main() {\n\n test1(123,456); // Testing out the overloaded functions\n test1({'a','b'}); // This works correctly with no ambiguity complaint\n // Neither GCC nor CLang mistake the initializer list of\n // two chars as possibly being two ints instead.\n\n Test2(123,456); // Testing out the …Run Code Online (Sandbox Code Playgroud) 运行此代码时,我可以期待什么行为:
do while(testA) {
// do stuff
} while(testB);
Run Code Online (Sandbox Code Playgroud)
它会表现得像:
do {
while(testA) {
// do stuff
}
} while(testB);
Run Code Online (Sandbox Code Playgroud)
要么:
if(testA) {
do {
// do stuff
} while(testA && testB);
}
Run Code Online (Sandbox Code Playgroud)
还是完全出乎意料的?
我问这个问题是因为我觉得这个问题很模糊,对于其他人来说这个话题,并不是因为我懒得去测试它.
我已经定义了两个通用函数
func job<T: Comparable>(x: T) {
println("1")
}
func job<T: Hashable>(x: T) {
println("2")
}
Run Code Online (Sandbox Code Playgroud)
当我试图打电话给其中一个时,例如:
let myInt: Int = 1 // Explicit Int just for clarity of the example
job(myInt)
Run Code Online (Sandbox Code Playgroud)
当然Swift抱怨并抛出一个错误
模糊地使用'job'
这是可以理解的,因为我不清楚我是否想要使用Comparable或Hashable(Int符合两者)
有没有办法可以提示编译器我要使用哪一个?