我想读取并从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) 我正在尝试使用boost :: program_options来读取包含多个部分的ini文件:
[slave]
address=localhost
port=1111
[slave]
address=192.168.0.1
port=2222
Run Code Online (Sandbox Code Playgroud)
有什么解决方案吗?
提前致谢!
当我试图用另一个线程唤醒一个线程时,我遇到了一个问题.简单的生产者/消费者的事物.
代码下方.第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) 我在多线程软件中使用openCV,并且正在启动尽可能多的可用CPU线程。此外,我正在使用一些openCV函数,然后我的问题就开始了。在我看来,openCV在内部启动自己的线程。
$ ps huH p 30266 | wc -l
1650
Run Code Online (Sandbox Code Playgroud)
1650个线程??? 停止openCV多线程支持的cmake标志是什么?
我不明白为什么我的编译器不接受下面的代码
#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++ 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)
我怎样才能实现这种运行,无锁和线程安全?
我的代码受线程" 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) 下面我展示了一个不编译的编辑灵魂员工示例.我想解决的问题是解析为不是结构的类.我知道,除了公共/私人之外,它是完全相同的.但是我需要在将类/结构存储到向量之前使用构造函数.如何更改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) 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) 带有以下代码段
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(线性或高斯)之间?
我想以类似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中?
我想检查用户输入的路径.输入必须提供不存在的文件的路径.此外,如果输入还提供目录,则这些目录必须存在.
示例输入:
/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)