我在PostgreSQL中有一个数据库.我有SQL查询(在PostgreSQL中btw工作得很好,所以sql代码没有错):
SELECT COUNT(*) as size, creation_date FROM item INNER JOIN raf_item USING (id) INNER JOIN item_detail USING (id) GROUP BY creation_date;
Run Code Online (Sandbox Code Playgroud)
其中创建日期定义为creation_date Date;PostgreSQL.例如,查询返回(取决于我在数据库中的内容):
size | creation_date
21 | 12-31-2012
18 | 04-03-2002
Run Code Online (Sandbox Code Playgroud)
我正在使用SOCI + C++从此查询中获取数据.我的整个C++代码:
#include <iostream>
#include <cstdlib>
#include <soci.h>
#include <string>
#include <postgresql/soci-postgresql.h>
using namespace std;
bool connectToDatabase(soci::session &sql, string databaseName, string user, string password)
{
try
{
sql.open(soci::postgresql, "dbname=" +databaseName + " user="+user + " password="+password);
}
catch (soci::postgresql_soci_error const & e)
{ …Run Code Online (Sandbox Code Playgroud) 从C中从最不重要开始的数字中获取数字非常简单:
#include <stdio.h>
int main()
{
int num = 1024;
while(num != 0)
{
int digit = num % 10;
num = num / 10;
printf("%d\n", digit);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是如何从第一个数字(此处为1)开始提取数字,该解决方案可以应用于任何数字?
对于数组来说这是微不足道的,但我不想使用数组,我不想使用逻辑运算符.
我在 Pascal 中实现了 Newton-Raphson 方法。这很奇怪,因为相同的代码在 C++ 中给出了很好的结果(对于 9,它是 3),但在 Pascal 中,对于 9,它是 3.25,为什么会这样呢?
帕斯卡:
Program NewtonRaphsonIter(output);
{$mode objFPC}
function newton_raphson_iter(a: real; p: real; eps: real; max_i: integer) : real;
var
x: real;
i: integer;
begin
x := a / 2.0;
i := 0;
repeat
x := (x + a / x) / 2.0;
i := i + 1;
if (x * x = a) then break;
if (i >= max_i) then break;
until abs(x - a / x) > eps; …Run Code Online (Sandbox Code Playgroud) 我想在Linux上用C创建一个目录树.我写了那段代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
static int dirExists(const char *path)
{
struct stat info;
if(stat( path, &info ) != 0)
return 0;
else if(info.st_mode & S_IFDIR)
return 1;
else
return 0;
}
int main(int argc, char **argv)
{
const char *path = "./mydir/firstdir/";
if(!dirExists(path))
{
mode_t mask = umask(0);
if(mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) == -1)
exit(-1);
umask(mask);
}
printf("%d\n", dirExists(path));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当a path是单个目录时,它可以,path = "./mydir"但是当我想创建一个目录树时,例如:path = "./mydir/a/b/c/d/"dirs不会被创建.为什么?
我有一个功能,可以说,callMe(args)。我希望能够同时调用我的函数 100 次(并发编程)。我的函数是用普通 C 语言编写的。我想让它同时适用于 Windows 和 Linux。
标准 C 库中是否有任何内置支持?或者你能提供一些建议,我应该如何在 Linux/Windows 中做到这一点?
这pthread对 Linux 环境有好处吗?
我有一个基础和派生类的应用程序.我需要在派生类中有一个基类的字段,但是在初始化它时会遇到一些问题.这是代码:
#include <iostream>
using namespace std;
class X
{
public :
X( int x ) { }
} ;
class Y : public X
{
X x ;
Y* y ;
Y( int a ) : x( a ) { }
} ;
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
而错误:
/tmp/test.cpp||In constructor ‘Y::Y(int)’:|
/tmp/test.cpp|14|error: no matching function for call to ‘X::X()’|
/tmp/test.cpp|14|note: candidates are:|
/tmp/test.cpp|7|note: X::X(int)|
/tmp/test.cpp|7|note: candidate expects 1 argument, 0 provided|
/tmp/test.cpp|4|note: X::X(const X&)|
/tmp/test.cpp|4|note: candidate expects 1 …Run Code Online (Sandbox Code Playgroud) 我想找到在array.For情况下两个相邻值之间的差异最大,为阵列int tab[6] = {1,2,8,4,5,6};的最大差异是6因为之间的差异,8以及2.剩下的差异等于1.所以,我的程序的结果应该是6.但是,我的程序打印1,我不知道问题出在哪里:
#include <stdio.h>
int main(int argc, char **argv)
{
int n = 6;
int tab[6] = {1,2,8,4,5,6};
int diff = tab[1] - tab[0], maxdiff = diff, i;
for(i=2; i<n-1; i++)
{
if(diff > maxdiff)
maxdiff = diff;
diff = tab[i] - tab[i-1];
}
printf("%d\n", diff);
return 0;
}
Run Code Online (Sandbox Code Playgroud) c ×4
c++ ×3
linux ×2
arrays ×1
concurrency ×1
inheritance ×1
makefile ×1
mkdir ×1
pascal ×1
postgresql ×1
soci ×1
windows ×1