使用g ++ 4.7.0(-Wall -Wextra -Werror -Wconversion -std=c++11
)编译此代码:
#include <iostream> // std::cout, std::endl
#include <string> // std::string
#include <utility> // std::move
void out(std::string const &message)
{
static int count{0};
std::cout << count++ << " = " << message << std::endl;
}
struct Foo
{
Foo() {out("constructor");}
~Foo() {out("destructor");}
Foo(Foo const &) {out("copy constructor");}
Foo & operator=(Foo const &) {out("copy via assignment"); return *this;}
Foo(Foo &&) {out("move constructor");}
Foo & operator=(Foo &&) {out("move via assignment"); return *this;}
};
int main() …
Run Code Online (Sandbox Code Playgroud) 这是我对Project Euler问题#5的解决方案:
#include <stdio.h>
#include <stdint.h>
#define N 20
int main( int argc, char* argv[] )
{
uint64_t r = 1;
uint64_t i, j;
for( i = 2; i <= N; ++i )
if( (j = r%i) )
r *= ( (i%j) ? i : (i/j) );
printf( "\n%llu\n", r );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它具有O(n)效率.我查看了包含各种解决方案的官方帖子的几页,但我没有注意到O(n)或更低的效率.如果我只是简单地实现一些已知的解决方案,我不会感到惊讶,但如果我是,我找不到它.思考?