我的任务很简单:在Linux上用C++读取和解析一个大文件.有两种方法:
逐字节解析.
while(/*...*/) {
... = fgetc(...);
/* do something with the char */
}
Run Code Online (Sandbox Code Playgroud)缓冲区解析缓冲区.
while(/*...*/) {
char buffer[SOME_LARGE_NUMBER];
fread(buffer, SOME_LARGE_NUMBER, 1, ...);
/* parse the buffer */
}
Run Code Online (Sandbox Code Playgroud)现在,逐字节解析对我来说更容易(不检查缓冲区的满载程度等).但是,我听说读大片更有效率.
什么是哲学?是"最佳"缓冲内核的任务,所以当我打电话时它已经被缓冲了fgetc()?或者是否建议我处理它以获得最佳效率?
此外,除了所有哲学:Linux上的现实是什么?
最小的例子:
class A
{
friend void swap(A& first, A& second) {}
void swap(A& other) {}
void call_swap(A& other)
{
swap(*this, other);
}
};
int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)
g ++ 4.7说:
friend.cpp: In member function ‘void A::call_swap(A&)’:
friend.cpp:7:20: error: no matching function for call to ‘A::swap(A&, A&)’
friend.cpp:7:20: note: candidate is:
friend.cpp:4:7: note: void A::swap(A&)
friend.cpp:4:7: note: candidate expects 1 argument, 2 provided
Run Code Online (Sandbox Code Playgroud)
退出第4行:
// void swap(A& other) {}
Run Code Online (Sandbox Code Playgroud)
......它工作正常.如果我想保留交换功能的两种变体,为什么以及如何解决这个问题?
以下gnuplot代码工作正常:
plot 'data.txt' using 2:4 ' %lf %lf %lf %lf' title "1 PE" with linespoints;
Run Code Online (Sandbox Code Playgroud)
在下面的代码中,我想说:"使用第4列中的数字,然后将其除以第3列中的数字".或者:"使用第2列中的数字,但将其除以常数2.0".以下代码演示了我尝试实现的内容,但它不起作用.
plot 'data.txt' using 2:4/4 ' %lf %lf %lf %lf' title "1 PE" with linespoints;
plot 'data.txt' using 2:4/2.0 ' %lf %lf %lf %lf' title "1 PE" with linespoints;
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?
对于给定的数字x,y并且n,我想计算x-y mod n在C.在这个例子来看一下:
int substract_modulu(int x, int y, int n)
{
return (x-y) % n;
}
Run Code Online (Sandbox Code Playgroud)
只要x>y,我们没事.然而,在另一种情况下,modulu操作是未定义的.
你可以想到x,y,n>0.我希望结果是正的,所以如果(x-y)<0,那么((x-y)-substract_modulu(x,y,n))/ n应该是一个整数.
您知道的最快算法是什么?有没有避免任何电话if和operator??
最小的工作示例.
#include <cassert>
#include <list>
#include <queue>
//#define USE_PQ
struct MyClass
{
const char* str;
MyClass(const char* _str) : str(_str) {}
MyClass(MyClass&& src) { str = src.str; src.str = nullptr; }
MyClass(const MyClass&) = delete;
};
struct cmp_func
{
bool operator() (const MyClass&, const MyClass&) const
{
return true;
}
};
typedef std::priority_queue<MyClass, std::vector<MyClass>, cmp_func> pq_type;
#ifdef USE_PQ
MyClass remove_front(pq_type& l)
{
MyClass moved = std::move(l.top());
// error from the above line:
// use of deleted function ‘MyClass::MyClass(const MyClass&)’
l.pop(); …Run Code Online (Sandbox Code Playgroud) 例:
struct s { int a; };
s func() { return {42}; }
int main() {
s new_obj = func(); // line 6
(void) new_obj;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这有效.现在,如果我们假设我们的编译器没有RVO会发生什么?
func返回一个struct的结构s,因此{42}必须转换为s,然后返回并最终复制到new_obj第6行.func 返回初始化列表,因此无法进行深层复制.语言是什么意思?你能举一个证明吗?
注意:我知道这在这个例子中看起来没用,但是对于返回非常大的常量std::arrays,我不想依赖RVO.
元组有点像结构.是否还有像工会一样的元组?或者我可以在元组中访问成员的工会,例如
my_union_tuple<int, char> u;
get<1>(u);
get<int>(u); // C++14 only, or see below
Run Code Online (Sandbox Code Playgroud)
对于第二行,请参见此处.
当然,解决方案不仅适用于特定的联合<int, char>,而且适用于任意类型和数量的类型.
看看这段代码.
#include <vector>
template<class ...Args>
using other_vector = std::vector<Args...>;
template<class T>
void f(std::vector<T>& ) {}
template<class T>
void f(other_vector<T>& ) {}
int main()
{
other_vector<int> b;
f(b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它没有编译,因为f正在重新声明.我完全理解错误.但是,我需要一个行为类似的第二个类std::vector<T>,但将被视为一个不同的类型,因此重载,如上例所示,是合法的.
我能做什么?
std::vector<T>作为基类.这可能有效,但不应该从std容器继承.有更好的选择吗?允许使用C++ 11或C++ 14.
这个最小的例子编译没有警告和运行:
// library
template<class T, T t> struct library_struct {};
// user
enum class my_enum { x, y, z };
int main()
{
library_struct<my_enum, my_enum::x> unused; // l.7
(void) unused;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,我希望编译器my_enum从枚举模板参数中推导出类型模板参数my_enum::x.这样看起来会更好:
library_struct<my_enum::x> unused;
Run Code Online (Sandbox Code Playgroud)
我见过编译器能够推导出模板参数的例子,但我只允许省略模板参数列表中的最后一个模板参数.那么可以在这里省略枚举类型吗?
编辑:我对没有宏的解决方案感兴趣.
互斥体在许多编程语言中很常见,例如C/C++.我想念他们用Java.但是,我可以通过多种方式编写自己的方法class Mutex:
什么是最快(最好的运行时)方式?我认为同步是最常见的,但性能呢?