小编Gal*_*lik的帖子

为什么std :: istringstream似乎在三元(?:)运算符中以不同的方式解析为std :: ifstream?

我习惯于编写带有文件名或读取的小命令行工具std::cin,所以我一直在使用这种模式:

int main(int argc, char* argv[])
{
    std::string filename;

    // args processing ...

    std::ifstream ifs;

    if(!filename.empty())
        ifs.open(filename);

    std::istream& is = ifs.is_open() ? ifs : std::cin;

    std::string line;
    while(std::getline(is, line))
    {
        // process line...
    }

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

在阅读Stack Overflow上的一个问题之后,我尝试修改我常用的模式,以满足从文件或文件中读取的需要std::istringstream.令我惊讶的是它不会编译并给出这个错误:

temp.cpp:164:47: error: invalid initialization of non-const reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from an rvalue of type ‘void*’
      std::istream& is = ifs.is_open() ? ifs : iss; // won't compile
Run Code Online (Sandbox Code Playgroud)

在我看来,它试图将std::istringstreamobject(iss)转换为布尔值并获取它operator void*(). …

c++ conditional-operator c++11

25
推荐指数
3
解决办法
2292
查看次数

在std :: list <shared_ptr>的C++ 14初始化列表中间抛出GCC(但不是Clang)下的内存泄漏

考虑以下程序:

#include <stdexcept>
#include <stdio.h>
#include <memory>
#include <list>
class Foo {
public:
    Foo(){
        if (s_ct==0) {throw std::bad_alloc();}
        --s_ct;
        fprintf(stderr, "ctor %p\n", this);
    }
    ~Foo(){
        fprintf(stderr, "dtor %p\n", this);
    }
private:
    static int s_ct;
};

int Foo::s_ct = 2;

int main(){
    try {
        std::list<std::shared_ptr<Foo>> l = {
            std::make_shared<Foo>(),
            std::make_shared<Foo>(),
            std::make_shared<Foo>()
        };
    } catch (std::bad_alloc&) {
        fprintf(stderr, "caught exception.\n");
    }
    fprintf(stderr, "done.\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译如下:

[little:~] $ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, …
Run Code Online (Sandbox Code Playgroud)

c++ exception-handling shared-ptr initializer-list c++14

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

迭代器使用end()持续std :: vector的元素 -

我有一个std::vector,我想要iterator向量中的最后一个元素; 我将存储此迭代器供以后使用.

注意:我想要一个迭代器引用它,而不是std::vector::back.因为我希望能够从std::vector::begin后面开始计算这个对象的索引.

以下是我将迭代器放到最后一个元素的逻辑:

std::vector<int> container;
std::vector<int>::iterator it = container.end()--;
Run Code Online (Sandbox Code Playgroud)

由于std::vector::end具有O(1)时间复杂度,有没有更好的方法来做到这一点?

c++ stl vector c++11

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

如何在Swift中将十六进制数转换为bin?

我有字符串变量:var str ="239A23F"如何将此字符串转换为二进制数? str.toInt()不起作用.

binary hex swift

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

基于构造函数的可访问性在函数之间进行选择的模板方法

我正在编写一个类ptr_scope_manager来管理给定范围内指针的创建和销毁.我研究了这个问题的答案:

私有构造函数禁止使用emplace [_back]()来避免移动

看来,如果我想管理一个对象的类创建私有构造函数,我的内部std::vector可以使用push_back但不能emplace_back构造对象.这是因为emplace_back使用内部类来构造对象.这意味着友情ptr_scope_manager不足以允许它创建具有私有构造函数的对象.

所以我所做的是制作两个create方法,一个用于具有公共构造函数的对象,另一个用于具有私有构造函数的对象ptr_scope_manager.

template<typename Type>
class ptr_scope_manager
{
private:
    std::vector<Type> ptrs;

public:
    template<typename... Args>
    Type* create_private(Args... args)
    {
        ptrs.push_back(Type(args...));
        return &ptrs.back();
    }

    template<typename... Args>
    Type* create_public(Args... args)
    {
        ptrs.emplace_back(args...);
        return &ptrs.back();
    }
};

class public_ctor
{
    int i;
public:
    public_ctor(int i): i(i) {} // public
};

class private_ctor
{
    friend class ptr_scope_manager<private_ctor>;
    int i;
private:
    private_ctor(int i): i(i) {} // …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae c++11

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

获取可执行文件的路径(使用 std::filesystem)

在我被标记为重复之前,我能找到的所有类似问题在引入 std::filesystem 之前都有答案,并且使用特定于平台的代码或 Boost::filesystem。我正在寻找一个使用 std::filesystem 的便携式答案。


是否可以使用 std::filesystem 获取 C++ 可执行文件所在的路径(而不是工作目录)?如果是的话,怎么样?

c++ c++17 std-filesystem

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

C++中的字符串标记生成器,允许多个分隔符

有没有办法在C++中用多个分隔符标记字符串?在C#中,我会做到:

string[] tokens = "adsl, dkks; dk".Split(new [] { ",", " ", ";" }, StringSplitOptions.RemoveEmpty);
Run Code Online (Sandbox Code Playgroud)

c# c++ string tokenize

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

如何捕获I/O异常(确切的I/O,而不是std :: exception)

我从这里尝试了示例程序(使用mingw-w64).该计划崩溃了.所以我编辑了它:

#include <iostream>     // std::cerr
#include <fstream>      // std::ifstream

int main()
{
    std::ifstream file;
    file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    try {
        file.open("not_existing.txt");
        while (!file.eof())
            file.get();
        file.close();
    }
    catch (std::ifstream::failure e) {
        std::cerr << "Exception opening/reading/closing file\n";
    }
    catch (const std::exception& e) {
        std::cerr << "should not reach this";
    }

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

现在它运行,但打印should not reach this,而我期待它打印Exception opening/reading/closing file.

为什么我的期望错了?

编辑:因为这似乎是一个重点,这是我的编译器的确切版本:mingw-w64版本"x86_64-6.2.0-posix-sjlj-rt_v5-rev1",即GCC版本6.2

c++ io exception-handling exception

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

向量矢量范围的范围

假设我有一系列名为rng的T.我可以

auto groups = ranges::view::group_by(rng, bin_op);
Run Code Online (Sandbox Code Playgroud)

群体现在是T的范围.

我也可以这样做

auto groups = ranges::view::group_by(rng, bin_op) | ranges::to_vector;
Run Code Online (Sandbox Code Playgroud)

得到T的范围向量.不过这个

auto groups = ranges::view::group_by(rng, bin_op)
            | ranges::to_vector
            | ranges::action::transform([] (auto r) { return r | ranges::to_vector; };
Run Code Online (Sandbox Code Playgroud)

以及

auto groups = ranges::view::group_by(rng, bin_op)
            | ranges::to_vector
            | ranges::action::transform([] (auto r) { return std::vector<T>{} | ranges::action::push_back; };
Run Code Online (Sandbox Code Playgroud)

因为看起来range :: action :: transform()在这种情况下返回void并且"传递给action :: transform的函数的结果类型必须可写回源范围",因此不起作用.

那么如何将范围范围转换为矢量矢量?

注意:对不好的标签很抱歉,但我找不到范围/ range-ts/ranges-v3标签,我不允许创建一个标签,也不能在标题中使用它.

c++ range-v3

7
推荐指数
2
解决办法
620
查看次数

如何比较time_t和std :: filesystem :: file_time_type

我正在将一些代码转换boost::filesystemstd::filesystem.使用的先前代码boost::filesystem::last_write_time()返回time_ttime_t我已经持有的对象如此直接的比较是微不足道的.顺便说一下,这个time_t我持有的文件内容很久以前就已经存在了,所以我坚持使用这个"自unix epoch以来的时间"类型.

std::filesystem::last_write_time返回一个std::filesystem::file_time_type.是否有可移植的方式将a转换file_time_type为a time_t或以其他方式可比较两个对象?

#include <ctime>
#include <filesystem>

std::time_t GetATimeInSecondsSince1970Epoch()
{
    return 1207609200;  // Some time in April 2008 (just an example!)
}

int main()
{
    const std::time_t time = GetATimeInSecondsSince1970Epoch();
    const auto lastWriteTime = std::filesystem::last_write_time("c:\\file.txt");

    // How to portably compare time and lastWriteTime?
}
Run Code Online (Sandbox Code Playgroud)

编辑:请注意cppreference.comlast_write_time上的示例代码表示它假设时钟是std::chrono::system_clock实现该to_time_t功能的状态.这个假设并不总是如此,并不在我的平台上(VS2017).

c++ boost-filesystem c++-chrono

7
推荐指数
3
解决办法
1708
查看次数