标签: std

Java中的C的stdlib rand()函数

我使用srand()和rand()从stdlib 生成了一个来自C中已知种子的一系列随机数.我现在需要使用Java中的C相同的种子生成相同的数字序列.

Java的Random类文档声明它使用"线性同余公式".我发现的文档rand()说它使用了"线性同余"生成器,虽然我不确定这是否适用于一个特定的实现.

有没有人知道如果给定相同的种子或者Java 的端口srand()和rand()存在,两个生成器是否会生成相同的数字?

c java random std

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

如何修改C++ runtime_error的字符串?

我有一个继承自std::runtime_error这样的类:

#include <string>
#include <stdexcept>

class SomeEx : public std::runtime_error
{
public:
    SomeEx(const std::string& msg) : runtime_error(msg) { }
};
Run Code Online (Sandbox Code Playgroud)

表示msg总是类似"无效的类型ID 43".有没有办法用另一个构造函数(或另一个方法)构建"什么字符串",以便我只提供整数类型ID?就像是:

SomeEx(unsigned int id) {
    // set what string to ("invalid type ID " + id)
}
Run Code Online (Sandbox Code Playgroud)

c++ runtime-error exception std

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

std :: array c ++ 11初始化语法错误

std :: array即将获得

no match for ‘operator=’ in ‘myarr = {1, 5, 2, 3, 4}’
Run Code Online (Sandbox Code Playgroud)

编译此代码时出错

#include <iostream>
#include <array>

using namespace std;

int main(int argc, char const *argv[])
{
    array<int, 5> myarr;
    myarr = {1,5,2,3,4};

    for(auto i : myarr)
    {
        cout << i << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我在同一条线上进行编译时它会编译

array<int, 5> myarr = {1,5,2,3,4};
Run Code Online (Sandbox Code Playgroud)

如何在分隔线上分配值

我需要在类构造函数中赋值,我该怎么办?

class myclass
{
  myclass()
  {
    myarr = {1,2,3,4,5}; /// how to assign it   // it gives errors
  }
};
Run Code Online (Sandbox Code Playgroud)

c++ arrays std operator-keyword c++11

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

为什么fstream方法返回对"*this"的引用?

我最近开始用C++编写代码,但我很长时间用C语言编写.所以我正在从fstream类中读取方法,并且我已经发现每个可能是一个过程的方法(什么都不返回)都返回一个引用到调用其方法的对象.(例如,fstream&fstream :: read(char_type*__s,streamsize __n)).

为什么这样做?

我在fstream类的顶部编写了一个小层,所以我想知道我是否应该在读取方法中返回一个引用.

谢谢.

c++ fstream std

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

C++ - stringstream <<"覆盖"

我正在用C++制作一个OpenGL游戏.与其他语言相比,我在C++方面相当无趣.无论如何,我为一些图像创建了一个带有"base"目录的字符串流.然后我将此stringstream作为函数参数传递给构造函数.构造函数附加图像文件名,然后尝试加载生成的路径.然而...

D:\CodeBlocks Projects\SnakeRoid\bin\Debug\Texts\ <-- before appending the filename
Ship01.tgacks Projects\SnakeRoid\bin\Debug\Texts\ <-- After.
Run Code Online (Sandbox Code Playgroud)

显然不正确!结果应为D:\ CodeBlocks Projects\SnakeRoid\bin\Debug\Texts\Ship01.tga

我的代码的相关部分:

std::stringstream concat;
std::string txtFullPath = "Path here";

...

concat.str(""); //Reset value (because it was changed in ...)
concat << texFullPath; //Restore the base path
PS = new PlayerShip(&TexMan, concat); //Call the constructor
Run Code Online (Sandbox Code Playgroud)

构造函数的代码

PlayerShip::PlayerShip(TextureManager * TexMan, std::stringstream &path)
{
    texId = 2;
    std::cout << path.str(); //First path above
    path << "Ship01.tga";
    std::cout  << path.str(); //Second - this is the messed up one
    //Do more …
Run Code Online (Sandbox Code Playgroud)

c++ std stringstream

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

给定程序中哪里错了?

这是我的程序,用于查找给定集的所有子集.为了解决它,我使用了递归.但是当我在windowsblocks上编译它时.它给

此应用程序已请求Runtime以不寻常的方式终止它.

并在gcc编译器中它没有显示任何答案,没有响应.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<string> findAllSubset(char c, vector<string> v) {
    int size = v.size();
    if(size == 1) {
        v.push_back("");
        return v;
    }
    c = v[size-1][0];
    v.pop_back();
    v = findAllSubset(c, v);

    for(int i = 0; i < v.size(); i++) {
        string s= "";
        if(v[i].size() == 0){
            s += c;
            v.push_back(s);
        }
        else {
            s += v[i] + c;
            v.push_back( s );
        }
    }
    return v;
}

main() {
    vector<string> v, ans;
    char c …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm recursion std

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

缺少std :: string功能

任何人都知道为什么C++标准库' std::string类(或更一般地说是std::basic_string类模板)缺少普通的字符串功能,如大写,子串替换和修剪等,与Qt的QString类或Python字符串相比?

c++ string qstring std

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

Mac OS X上Std Lib的来源

我最近开始在Mac OS X上编程C(10.7.4).我在/ usr/include中有stdlib的各种头文件,但希望得到源代码.这主要是我自己学习所需要的,但我也希望生成Ctags以便在Vim中轻松导航.

有可能从某个地方获得这个,如果是这样的话吗?

提前致谢!

c vi macos std ctags

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

std :: map,自定义键类型,仅对一个变量进行排序

我有一个密钥类型:

struct KeyT {
    uint32_t timestamp;

    // example!
    uint32_t a;
    uint32_t b;
    uint32_t c;
    uint32_t d;
    uint32_t e;
    // ...

    bool operator== (const KeyT& key) const
    {
        if(timestamp == key.timestamp && a == key.a && b == key.b && d == key.d && c == key.c && e == key.e)
            return true;
        return false;

    }
    bool operator< (const KeyT& key) const
    {
        if(timestamp < key.timestamp)
            return true;
        else if(timestamp == key.timestamp && a < key.a && b < key.b && …
Run Code Online (Sandbox Code Playgroud)

c++ std map operators

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

标准迭代器分配错误

如果问题很愚蠢,请耐心等待.

以下是在头文件中定义的:

typedef char NAME_T[40];

struct NAME_MAPPING_T
{
    NAME_T englishName;
    NAME_T frenchName;
};

typedef std::vector<NAME_MAPPING_T> NAMES_DATABASE_T;
Run Code Online (Sandbox Code Playgroud)

后来需要找到一个特定的英文名称:

const NAMES_DATABASE_T *wordsDb;

string str;

std::find_if(   wordsDb->begin(), 
                wordsDb->end(), 
                [str](const NAME_MAPPING_T &m) -> bool { return strncmp(m.englishName, str.c_str(), sizeof(m.englishName)) == 0; } );
Run Code Online (Sandbox Code Playgroud)

这段代码(我诚实地复制粘贴)编译,但如果我想检查find_if()返回的值,如下所示:

NAMES_DATABASE_T::iterator it;
it = std::find_if(blah ..)
Run Code Online (Sandbox Code Playgroud)

代码不会编译!

实际上,行 = std :: find_if(...) 将返回错误:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Vector_const_iterator<_Myvec>' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)

怎么了 ?

谢谢你的时间.

c++ iterator std find

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