我GetSystemTimeAdjustment在Windows 7上使用该功能运行了一些测试,得到了一些我无法解释的有趣结果.正如我所理解的那样,如果系统时间定期同步,则该方法应该返回,如果是,则在哪个时间间隔以及更新它的增量(请参阅MSDN上的GetSystemTimeAdjustment函数).
从此我可以看出,如果我查询系统时间,例如GetSystemTimeAsFileTime重复使用,我应该不做任何更改(系统时钟尚未更新),或者是检索到的增量的倍数的更改GetSystemTimeAdjustment.问题一:这个假设是否正确?
现在考虑以下测试代码:
#include <windows.h>
#include <iostream>
#include <iomanip>
int main()
{
FILETIME fileStart;
GetSystemTimeAsFileTime(&fileStart);
ULARGE_INTEGER start;
start.HighPart = fileStart.dwHighDateTime;
start.LowPart = fileStart.dwLowDateTime;
for (int i=20; i>0; --i)
{
FILETIME timeStamp1;
ULARGE_INTEGER ts1;
GetSystemTimeAsFileTime(&timeStamp1);
ts1.HighPart = timeStamp1.dwHighDateTime;
ts1.LowPart = timeStamp1.dwLowDateTime;
std::cout << "Timestamp: " << std::setprecision(20) << (double)(ts1.QuadPart - start.QuadPart) / 10000000 << std::endl;
}
DWORD dwTimeAdjustment = 0, dwTimeIncrement = 0, dwClockTick;
BOOL fAdjustmentDisabled = TRUE;
GetSystemTimeAdjustment(&dwTimeAdjustment, &dwTimeIncrement, …Run Code Online (Sandbox Code Playgroud) 由于我在文档中找不到任何内容,我想我在这里问.我有以下程序(C++ 11):
#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main () {
string tmp = " #tag #tag1#tag2 #tag3 ####tag4 ";
list<iterator_range<string::iterator> > matches;
split( matches, tmp, is_any_of("\t #"), token_compress_on );
for( auto match: matches ) {
cout << "'" << match << "'\n";
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
''
'tag'
'tag1'
'tag2'
'tag3'
'tag4'
''
Run Code Online (Sandbox Code Playgroud)
我原以为该token_compress_on选项会删除所有空标记.例如,解决方案是使用boost::trim_if.不过我想知道这是否是boost :: split的理想行为,以及为什么会这样?
(g ++ 4.6.3,提升1.48)
我需要在很长一段时间(几个小时)内每隔几毫秒(20,30,40毫秒)获得准确的时间戳.采用时间戳的函数作为第三方库的回调调用.
使用GetSystemTime()一个可以得到正确的系统时间戳,但只有毫秒精度,这对我来说不够精确.使用会QueryPerformanceTimer()产生更准确的时间戳,但与系统时间戳长期不同步(请参阅http://msdn.microsoft.com/en-us/magazine/cc163996.aspx).
上面链接的站点提供的解决方案只能在旧计算机上运行,当我尝试与较新的计算机一起使用时,它会在同步时挂起.
在我看来,提升也只是在毫秒精度上工作.如果可能的话,我想避免使用外部库,但如果没有其他选择,我会继续使用它.
有什么建议?
谢谢.
假设我有以下文件:
#ifndef A_H
#define A_H
#include <vector>
class A {
public:
static int add( int x );
static int size();
private:
static std::vector<int> vec;
};
#endif
Run Code Online (Sandbox Code Playgroud)
#include "A.h"
std::vector<int> A::vec;
int A::add( int x ) {
vec.push_back( x );
return vec.size();
}
int A::size() {
return vec.size();
}
Run Code Online (Sandbox Code Playgroud)
#ifndef B_H
#define B_H
class B {
public:
static const int val = 42;
};
#endif
Run Code Online (Sandbox Code Playgroud)
#include "B.h"
#include "A.h"
int tempvar = A::add( B::val );
Run Code Online (Sandbox Code Playgroud)
假设我有一个枚举,我希望每个枚举值与某个类型相关联.假设标准类型是double,如果我希望它是其他东西,我需要明确指定它.
Q1:这是实现这种事情的首选方式吗?
enum A {
v1,
v2,
v3
};
// for every value of A, the standard type is double
template<A a>
struct A_info {
typedef double type;
};
// other types for certain values can be specified using specialization
template<>
struct A_info<v2> {
typedef size_t type;
};
Run Code Online (Sandbox Code Playgroud)
然后,假设我有一些函数模板,我想根据与枚举值相关联的类型调用该函数:
template<typename T>
void foo() { /* do something */ }
template<A a>
void bar() {
foo< typename A_info<a>::type >();
}
Run Code Online (Sandbox Code Playgroud)
这很好用.现在假设,我有另一个功能,取决于可变参数模板列表,我想做类似上面的事情......
template<typename ... T>
void foo_multiple() { /* stuff */ } …Run Code Online (Sandbox Code Playgroud)