小编Ret*_*nja的帖子

rand()不服从Qt Creator中的srand()

我在Qt Creator 1.0.0(Qt版本4.5.0)中编写了一个程序,在main()函数的开头我放了

srand(time(0));
Run Code Online (Sandbox Code Playgroud)

然后我从另一个线程(QThread的子类)调用rand().在该函数中,每次运行程序时,rand()都会生成相同的数字序列.我没有在一秒钟内多次运行该程序.

为什么会这样?

c++ qt multithreading

4
推荐指数
1
解决办法
1788
查看次数

为什么clang会拒绝gcc接受的这个unordered_set定义?

我希望unordered_set用我自己的哈希函数进行测试:

#include<unordered_set>
#include<iostream>
#include<functional>
using namespace std;
struct node{
    size_t value;
    bool operator == (const node& n){return value == n.value;}
};
size_t h(const node& n){
    return n.value;
}
int main(){
    unordered_set<node, std::function<size_t(const node&)>> s2(3,h);//failed
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我试图编译它,而clang给出了大量的错误:

clang++ m.cpp -std=c++11
In file included from m.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_set:324:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:659:21: error: invalid operands to binary
    expression ('const node' and 'const node')
        {return __x == __y;}
                ~~~ ^  ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__hash_table:2175:32: note: in instantiation of member
    function 'std::__1::equal_to<node>::operator()' …
Run Code Online (Sandbox Code Playgroud)

c++ hash compilation clang unordered-set

4
推荐指数
2
解决办法
165
查看次数

使用文件描述符读取 C 中的文件

我想通过使用文件描述符来读取文件。由于分配规则,我无法使用它的名称。

我通过打电话获得它open并且工作正常。此时我知道我必须使用该read()函数才能读取它。我的问题是该read()函数需要作为参数读取的字节数,并且我想每次从文件中读取整行,所以我不知道要读取多少字节。

例如,如果我使用fscanf(),它可以很好地处理一个简单的字符串,并且我可以根据需要收回整行。所以我的问题是:

是否有类似的函数fscanf()可以使用文件描述符而不是文件指针来调用?

c file file-descriptor

3
推荐指数
1
解决办法
1万
查看次数

使用带自定义rng的std :: shuffle?

我看到std :: shuffle采用URNG参数.我正在使用自定义版本的生成器(WELL512a),并希望将它与std :: shuffle一起使用.

我的问题是:是否可以将它与std :: shuffle一起使用?例如,可能使用URNG作为基类?

谢谢!

c++ shuffle std prng c++11

2
推荐指数
1
解决办法
905
查看次数

试图学习c ++中的基本文件操作

我正试图搞定处理文件C++.我正在尝试从一个文件中读取并使用相同的内容创建另一个文件.我已经成功了,我可以将文件的第一行复制而不是其余部分.谁能告诉我我做错了什么?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char * argv[]){

    string line;
    ofstream writeFile;
    ifstream readFile;
    readFile.open("students.txt");
    if (readFile.is_open()){
        while (getline (readFile, line)){
            writeFile.open("copytext.txt");
            writeFile << line;
            writeFile << line;
            writeFile << line;
            writeFile << line;
        }
    }
    readFile.close();
    writeFile.close();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
360
查看次数

致命错误 C1083:无法打开包含文件:“fstream.h”:没有这样的文件或目录

您好,我正在使用 Visual C++ 2010 Express,当我编译此示例程序时收到该错误,我下载了“致命错误 C1083:无法打开包含文件:'fstream.h':没有这样的文件或目录”

#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
Run Code Online (Sandbox Code Playgroud)

我尝试放入 using namespace std; 在代码块的开头,但我仍然收到错误

c++

2
推荐指数
1
解决办法
4万
查看次数

如何通过c ++从整个路径中获取文件名

问题:
我知道我可以通过以下方式获取文件名:

std::string wholePath = "/User/home/Lib/hello.cpp.h";
std::regex e(".*\\/(.*)\\..*$");
std::smatch sm;
std::regex_match(wholePath.cbegin(), wholePath.cend(), sm, e);

std::cout << "File Name is : " << sm[1];
Run Code Online (Sandbox Code Playgroud)

但我不知道如何从中获取文件名:

std::string wholePath = "\User\home\Lib\hello.cpp.h";
std::regex e_1(".*\(.*)\\..*$");
std::regex e_2(".*\\(.*)\\..*$");
std::regex e_3(".*\\\(.*)\\..*$");
std::regex e_4(".*\\\\(.*)\\..*$");
std::smatch sm;
// std::regex_match(wholePath.cbegin(), wholePath.cend(), sm, e);
Run Code Online (Sandbox Code Playgroud)

我已经尝试了上述四个表达式,但它们都不起作用。
我的问题,如何匹配字符'\'。
帮助 /。\

c++ regex

1
推荐指数
1
解决办法
2019
查看次数

Visual Studio 无缘无故地给出不明确的错误

它给出以下错误:

error C2872: 'count' : ambiguous symbol

Count 变量已被声明为全局变量,并且代码在 Sublime Text 中编译并运行。不明白为什么 Visual Studio 会为此哭泣。

#include <iostream>
#include <fstream>

using namespace std;

int** am; // Adjacency matrix
int* ar, * ar2; // Arrays to work with
int n; // Number of nodes
int node1, node2, k; // For reading input from the console
int count;

bool checkReachability(int, int, int);
void fillArray(int);
void updateArray(int,int);
void freeMemory();

int main() {

    ifstream in;
    in.open("Input2.txt");
    int a, b;
    if(in.is_open()) {
        in >> n;

        // …
Run Code Online (Sandbox Code Playgroud)

c++ visual-studio

0
推荐指数
1
解决办法
9414
查看次数

我如何创建一个类并声明另一个类的变量

我正在尝试使用DHT传感器

WifiConfig.h

#include "DHT.h"
class WifiConfig
{
  public:
    WifiConfig();
    std::unique_ptr<DHT> dht;
    void initialize(char const *ssid, char const *psk);

}
Run Code Online (Sandbox Code Playgroud)

WifiConfig.cpp

#include "WifiConfig.h"
WifiConfig::WifiConfig() {}

void WifiConfig::initialize(char const *ssid, char const *psk) {
   dht.reset(new DHT(DHTPin, DHT11));
   dht.readTemperature(); // doesnt work
}
Run Code Online (Sandbox Code Playgroud)

以这种方式在我的班级中使用另一个班级的正确方法是什么?

c++ arduino

0
推荐指数
1
解决办法
48
查看次数

*a++ = *b++(这是什么意思,它是如何工作的)

执行此操作后,值会是多少?

#include <stdio.h>
int main() {
    int *a = 0;
    int *b = 3;
    *a++ = *b++;
    printf("%d", a);
    printf("%d", b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码给了我一个分段错误。

c pointers post-increment

-2
推荐指数
1
解决办法
166
查看次数

如何从函数返回字符串

我想完成任务,定义特定月份的天数,对于此任务,我使用日期和时间库来获取当前月份,然后我想检查当月的天数.

我收到这个错误:

没有合适的构造函数可以从"char"转换为"std :: basic_string,std :: allocator>"

string daysInMonth(int month, string months);
time_t tt = system_clock::to_time_t(system_clock::now());
    struct tm * ptm = localtime(&tt);
    char buff[100];

    int days;
    string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int month =  ptm->tm_mon+1;


    switch (month)
    {
        case May: {
            days = 31;
            cout << daysInMonth(month, months);

    }
    }

string daysInMonth(int month, string months) {
    for (int i = 0; i < sizeof(months) / sizeof(months[0]); i++)
    {
        if (month …
Run Code Online (Sandbox Code Playgroud)

c++

-3
推荐指数
1
解决办法
67
查看次数