"无限"IEnumerable的一个简单例子
IEnumerable<int> Numbers() {
int i=0;
while(true) {
yield return unchecked(i++);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道
foreach(int i in Numbers().Take(10)) {
Console.WriteLine(i);
}
Run Code Online (Sandbox Code Playgroud)
和
var q = Numbers();
foreach(int i in q.Take(10)) {
Console.WriteLine(i);
}
Run Code Online (Sandbox Code Playgroud)
两者都工作正常(并打印数字0-9).
但复制或处理表达式时是否有任何陷阱q?我可以依赖这样一个事实,即它们总是被评估为"懒惰"吗?产生无限循环有危险吗?
以下算法可以对三个变量进行排序x,y并使用以下z类型K进行比较operator<:
void sort2(K& x, K& y) {
if(y < x)
swap(x, y);
}
void sort3(K& x, K& y, K& z) {
sort2(x, y);
sort2(y, z);
sort2(x, y);
}
Run Code Online (Sandbox Code Playgroud)
这需要在"最坏情况"中进行三次交换.然而,基础数学告诉我们,三个值的排序只能使用两个交换来完成.
示例:值(c,b,a)将使用三个交换进行排序:(c,b,a) - >(b,c,a) - >(b,a,c) - >(a,b, C).然而,一次交换就足够了:(c,b,a) - >(a,b,c).
什么是最简单的算法,在所有情况下最多两次掉期对三个变量进行排序?
如何std::function在需要C风格回调的函数中使用?
如果这是不可能的,那么下一个最好的事情是什么?
例:
// --- some C code I can not change ---
typedef void(*fun)(int);
void register_callback(fun f) {
f(42); // a test
}
// ------------------------------------
#include <functional>
#include <iostream>
void foo(const char* ptr, int v, float x) {
std::cout << ptr << " " << v << " " << x << std::endl;
}
int main() {
std::function<void(int)> myf = std::bind(&foo, "test", std::placeholders::_1, 3.f);
register_callback(myf); // <-- How to do this?
}
Run Code Online (Sandbox Code Playgroud) 我有两个非托管指针,IntPtr并希望在它们之间复制数据.我怎样才能做到这一点?我知道这个方法Marshal.Copy,但它只能在非托管和托管之间进行复制.第二部分:使用memcpy从C#中复制非托管数据比在非托管C/C++中复制更慢吗?
编辑:我会对平台独立实现特别感兴趣.
我想要一个容器
StdStyleSet<A>
Run Code Online (Sandbox Code Playgroud)
的
class A : IComparable<A> { ... }
Run Code Online (Sandbox Code Playgroud)
它满足std :: set的属性.这尤其是:
#include <iostream>
#include <vector>
int main()
{
class Int {
public:
Int(int _i) : i(i) {}
private:
int i;
};
std::vector<Int> VI;
}
Run Code Online (Sandbox Code Playgroud)
我尝试编译上面的代码并得到以下错误消息:
foo.cc: In function 'int main()':
foo.cc:13: error: 'main()::Int' uses local type 'main()::Int'
foo.cc:13: error: trying to instantiate 'template<class _Alloc> class std::allocator'
foo.cc:13: error: template argument 2 is invalid
foo.cc:13: error: invalid type in declaration before ';' token
Run Code Online (Sandbox Code Playgroud)
你们中的任何人都可以告诉我为什么我不能在C++中做这样的事情吗?提前致谢.
我有几个项目都依赖于基础库.现在当我在这个基本库中更改头文件时,我必须重建所有依赖项目.目前Eclipse/CDT构建了一个又一个项目.如何并行构建所有这些项目?
请注意,我已经为每个项目使用-j(并行编译)选项.但这还不够,因为:
我有一个
class A {
public int X;
public double Y;
public string Z;
// and more fields/properties ...
};
Run Code Online (Sandbox Code Playgroud)
和a List<A> data和可以构建一个linq查询,例如
var q = from a in data where a.X > 20 select new {a.Y, a.Z};
Run Code Online (Sandbox Code Playgroud)
然后dataGridView1.DataSource = q.ToList();在我的DataGridView中显示选择.
现在的问题是,是否可以从用户在运行时输入的文本构建查询?喜欢
var q = QueryFromText("from a in data where a.X > 20 select new {a.Y, a.Z}");
Run Code Online (Sandbox Code Playgroud)
关键是,用户(具有编程技能)可以动态且自由地选择显示的数据.
以下C代码在做什么?
int i;
int* p = &i;
0[p] = 42;
Run Code Online (Sandbox Code Playgroud)
我会想,这不会是事件编译.但它甚至在没有分段错误的情况下执行.所以我想知道[]我错过了什么奇怪的运算符.
一个简单的例子:
template<typename _X> // this template parameter should be usable outside!
struct Small {
typedef _X X; // this is tedious!
X foo;
};
template<typename SomeSmall>
struct Big {
typedef typename SomeSmall::X X; // want to use X here!
SomeSmall bar;
X toe;
};
Run Code Online (Sandbox Code Playgroud)
有没有办法在不使用类中的typedef X的Small情况下访问模板参数Small?
c# ×4
c++ ×4
algorithm ×1
c ×1
c++11 ×1
callback ×1
eclipse ×1
eclipse-cdt ×1
enumeration ×1
linq ×1
templates ×1
yield-return ×1