小编use*_*451的帖子

从txt文件中读取并删除第一行(或最后一行)而不复制

我想读取并从txt文件中删除第一行(没有复制,这是一个巨大的文件).
我已经阅读了网络,但每个人都只是将所需的内容复制到一个新文件中.我做不到.

低于第一次尝试.由于没有删除行,此代码将被置于循环中.如果代码将在每个开头删除第一行文件,代码将到达结尾.

#include <iostream>
#include <string>
#include <fstream>
#include <boost/interprocess/sync/file_lock.hpp>

int main() {
    std::string line;
    std::fstream file;
    boost::interprocess::file_lock lock("test.lock");
    while (true) {
        std::cout << "locking\n";
        lock.lock();
        file.open("test.txt", std::fstream::in|std::fstream::out);
        if (!file.is_open()) {
            std::cout << "can't open file\n";
            file.close();
            lock.unlock();
            break;
        }
        else if (!std::getline(file,line)) {
            std::cout << "empty file\n"; //
            file.close();                // never
            lock.unlock();               // reached
            break;                       //
        }
        else {
            // remove first line
            file.close();
            lock.unlock();
            // do something with line
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ fstream getline stdstring boost-interprocess

6
推荐指数
2
解决办法
5369
查看次数

boost :: program_options和ini文件中的多个部分

我正在尝试使用boost :: program_options来读取包含多个部分的ini文件:

[slave]
address=localhost
port=1111

[slave]
address=192.168.0.1
port=2222
Run Code Online (Sandbox Code Playgroud)

有什么解决方案吗?

提前致谢!

c++ boost-program-options

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

C++ 11 - 无法使用std :: thread和std :: condition_variable唤醒线程

当我试图用另一个线程唤醒一个线程时,我遇到了一个问题.简单的生产者/消费者的事物.

代码下方.第85行是我不明白的原因,为什么它不起作用.生产者线程填充std :: queue并在消费者线程等待NOT std :: queue.empty()时调用std :: condition_variable.notify_one().

在此先感谢您的帮助

#include <mutex>
#include <condition_variable>
#include <queue>
#include <string>
#include <iostream>
#include <thread>

// request
class request :
    public std::mutex,
      public std::condition_variable,
      public std::queue<std::string>
{
public:
  virtual ~request();
};

request::~request()
{
}

// producer
class producer
{
public:
  producer(request &);

  virtual ~producer();

  void operator()();

private:
  request & request_;
};

producer::producer(request & _request)
:
request_(_request)
{
}

producer::~producer()
{
}

void
producer::operator()()
{
  while (true) {
    std::lock_guard<std::mutex> lock(request_);
    std::cout << "producer\n";
    request_.push("something"); …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading condition-variable c++11

5
推荐指数
2
解决办法
1778
查看次数

如何编译openCV以确保单线程?

我在多线程软件中使用openCV,并且正在启动尽可能多的可用CPU线程。此外,我正在使用一些openCV函数,然后我的问题就开始了。在我看来,openCV在内部启动自己的线程。

$ ps huH p 30266 | wc -l
1650
Run Code Online (Sandbox Code Playgroud)

1650个线程??? 停止openCV多线程支持的cmake标志是什么?

c++ multithreading opencv

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

在std :: unordered_map中使用模板化键

我不明白为什么我的编译器不接受下面的代码

#include <unordered_set>
#include <unordered_map>

template<class T>
using M = std::unordered_set<T>;

template<class T>
using D = M<T>;

template<class T>
using DM = std::unordered_map < typename M<T>::const_iterator    // Problem
                              , typename D<T>::const_iterator >; // Problem

int main(int argc, char ** argv)
{
    D<int> d;
    M<int> m;
    DM<int> dm; // Problem
}
Run Code Online (Sandbox Code Playgroud)

编译器命令是

clang++ -std=c++14 test.cpp -o test
Run Code Online (Sandbox Code Playgroud)

编译器错误消息摘录是

/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/hashtable_policy.h:85:11: error: 
      implicit instantiation of undefined template
      'std::hash<std::__detail::_Node_const_iterator<int, true, false> >'
        noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
Run Code Online (Sandbox Code Playgroud)

为什么不允许使用它typename M<T>::const_iterator作为键std::unordered_map

c++ templates unordered-map c++11

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

C++ 17/C++ 1z并行使用std :: for_each

我想使用C++ 17并行功能将a的每个元素除以std::vector一些常量,并将结果存储在另一个std::vector相同长度和(!!)顺序中.

例如

{6,9,12} / 3 = {2,3,4}
Run Code Online (Sandbox Code Playgroud)

我有一个没有编译的例子

#include <execution>
#include <algorithm>

template <typename T>
std::vector<T> & divide(std::vector<T> const & in)
{
  std::vector<T> out(in.size(), 0);

  float const divisor = 3;

  std::for_each
  ( std::execution::par_unseq
  , in.begin()
  , in.end()
  , /* divide each element by divisor and put result in out */ );

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

我怎样才能实现这种运行,无锁和线程安全?

c++ c++17

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

如何使用std :: mt19937避免相同的随机结果?

我的代码受线程" std :: mt19937需要预热吗? "的影响.执行时,生成两个具有相同内容的向量,这不是我想要的.我期望两个载体中的内容不同.我做错了什么以及如何解决?

输出:

0.251423
0.729274
-1.43542
0.251423
0.729274
-1.43542
Run Code Online (Sandbox Code Playgroud)

码:

#include <vector>
#include <algorithm>
#include <iostream>
#include <random>
#include <array>
#include <functional>
#include <iterator>

class functor1
{
public:
  functor1()
  {
    std::random_device device;
    std::array<int, std::mt19937::state_size> seeds;
    std::generate_n(seeds.data(), seeds.size(), std::ref(device));
    std::seed_seq sequence(std::begin(seeds), std::end(seeds));
    engine.seed(sequence);
  }

  double operator()()
  {
    std::normal_distribution<double> distribution(0.0, 1.0);
    return distribution(engine);
  }

private:
  std::mt19937 engine;
};

int main()
{
    functor1 f1;

    std::vector<double> v0;
    std::vector<double> v1;

    std::generate_n(std::back_inserter(v0), 3, f1);
    std::generate_n(std::back_inserter(v1), 3, f1);

    std::ostream_iterator<double> out(std::cout, "\n");

    std::copy(v0.begin(), v0.end(), out); …
Run Code Online (Sandbox Code Playgroud)

c++ random

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

解析成类(不是结构)

下面我展示了一个不编译的编辑灵魂员工示例.我想解决的问题是解析为不是结构的类.我知道,除了公共/私人之外,它是完全相同的.但是我需要在将类/结构存储到向量之前使用构造函数.如何更改BOOST_FUSION_ADAPT_STRUCT?

我怎么能让它运行?

// STD HEADER
#include <iostream>
#include <string>
#include <complex>

// BOOST HEADER
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

namespace client
{
    namespace qi = boost::spirit::qi;

    namespace ascii = boost::spirit::ascii;

    class employee
    {
    public:
        employee (
            int _age
          , std::string _surname
          , std::string _forename
          , double _salary
        );

    private:
        int age_;
        std::string surname_;
        std::string forename_;
        double salary_;
    };

    employee::employee (
        int _age
      , std::string _surname
      , std::string _forename
      , double _salary …
Run Code Online (Sandbox Code Playgroud)

c++ boost-spirit boost-fusion

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

递增std :: unordered_map的迭代器

7号线和8号线为什么不好?为什么输入/减少一些迭代器是不好的?

#include <unordered_map>

int main()
{
    std::unordered_multimap<int,int> myumm({{1,3},{3,2},{5,5},{0,9}});
    auto first = myumm.begin();
    auto second = first+1;      // bad
    auto third = --myumm.end(); // bad too
    auto fourth = myumm.end();
}
Run Code Online (Sandbox Code Playgroud)

c++ stl unordered-map

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

如何使用pandas / numpy标准化/标准化日期?

带有以下代码段

import pandas as pd
train = pd.read_csv('train.csv',parse_dates=['dates'])
print(data['dates'])
Run Code Online (Sandbox Code Playgroud)

我加载并控制数据。

我的问题是,如何使data ['dates']标准化/归一化以使所有元素都位于-1和1(线性或高斯)之间?

python numpy pandas

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

替换std :: transform,插入到std :: vector中

我想以类似std :: transform的方式将值插入到std :: vector中。std :: transform需要预先设置大小的第三个参数,但是在我的情况下,大小取决于transformers(),并且不可预测。

...
// std::vector<int> new_args(); <-- not working
std::vector<int> new_args(args.size());
std::transform(args.begin(),args.end(),new_args.begin(),transformers());
Run Code Online (Sandbox Code Playgroud)

是否有一种std:transform-ish方式将值插入到std :: vector中?

c++ stl stl-algorithm c++11

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

boost :: filesystem :: path.parent_path()和空格

我想检查用户输入的路径.输入必须提供不存在的文件的路径.此外,如果输入还提供目录,则这些目录必须存在.

示例输入:

/existent_dir_a/existent_dir_b/existent_file.bin < - false

/existent_dir_a/existent_dir_b/non_existent_file.bin < - 好的

/non_existent_dir_a/non_existent_file.bin < - false

non_existent_file.bin < - 好的

existent_file.bin < - false

下面的代码显示了一个示例.我希望它能实现我的目标,但我还有一个问题.

我怎么能摆脱output.parent_path().string().size() != 0它,因为它对我来说似乎有点难看?

#include <boost/filesystem.hpp>
#include <iostream>
#include <string>
#include <exception>

namespace fs = boost::filesystem;

int main(int ac, char ** av)
{
  fs::path output(av[1]);

  std::cout << output << std::endl;

  std::cout << output.parent_path() << std::endl;

  if (fs::exists(output))
  {
    std::string msg = output.string() + " already exists";

    throw std::invalid_argument(msg);
  }

  if ( output.parent_path().string().size() != 0 &&
       !fs::exists(output.parent_path()) …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-filesystem

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