我有一个字符串流,我想对其进行迭代并确定其中是否存在子字符串。
我知道我可以只转换为字符串并执行 std::string::find(),但我只是希望尽可能避免从 stringstream 转换为 string。
我了解以下内容不起作用,因为 istream_iterator 使用 char 作为其类型(不是字符串)
stringstream ssBody;
string sFindThis;
...
auto itr = std::find (
istreambuf_iterator<char>(ssBody),
istreambuf_iterator<char>(),
sFindThis
);
Run Code Online (Sandbox Code Playgroud)
但是我可以在不转换为字符串的情况下使用 std::find 或类似方法在 stringstream 中搜索字符串吗?
我正在尝试用 C++ 编写观察者模式,因此我有一个包含 eventname -> 回调函数向量的映射
回调函数存储在向量中,如下所示
std::function<void(void *)>
Run Code Online (Sandbox Code Playgroud)
所以地图看起来像
std::unordered_map<std::string, std::vector<std::function<void(void *)>>>
Run Code Online (Sandbox Code Playgroud)
我可以向向量添加侦听器并接收和响应事件通知。我的问题是实施分离。
所以 std::function 无法比较,因此擦除/删除已不再适用,所以我想搜索向量并手动比较。
我发现这个问题使用 std::function::target 成功地访问了底层指针,但我不能使用它,因为我使用 std::bind 来初始化回调:
std::function<void(void *)> fnCallback = std::bind(&wdog::onBark, this, std::placeholders::_1)
Run Code Online (Sandbox Code Playgroud)
我只想比较底层成员函数 ptr,甚至与该成员函数关联的底层对象 ptr 进行比较。有什么办法吗?
我想避免将成员 fn ptr 包装在包含哈希的对象中,尽管看起来我可能必须这样做......
我想知道是否可以使用 std::replace 将字符串流中的双引号替换为单引号。
我有:
std::replace(
std::ostreambuf_iterator<char>(ssScript),
std::ostreambuf_iterator<char>(),
'"', '\''
);
Run Code Online (Sandbox Code Playgroud)
但当然 ostreambuf_iterator 没有默认构造函数,因此无法编译。
是否有另一种方法可以像这样在内联字符串流中替换字符的出现?
我想在创建用户并指定其密码的远程虚拟机上执行命令。
使用 sshpass,我可以以 root 身份远程执行命令,例如
sshpass -p "rootpasswd" ssh root@remotevm '<remote command here>'
Run Code Online (Sandbox Code Playgroud)
所以我想创建一个用户,我可以用
adduser <username>
Run Code Online (Sandbox Code Playgroud)
接下来我想使用 usermod 创建密码:
usermod --password <encrypted_pass> <user>
Run Code Online (Sandbox Code Playgroud)
我希望能够指定密码香蕉 - 那么我如何加密字符串香蕉以便我可以将香蕉指定为用户的加密密码?
我正在使用 boost 属性树从 json 文件中读取值。
{
"some_values":
{
"field_1": "value_1",
"field_2": true
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方法读取值:
spTree->get<string>("some_values.field_1", "");
spTree->get<bool>("some_values.field_2", false);
Run Code Online (Sandbox Code Playgroud)
但是我可以读取存储在任何给定字段中的变量的类型吗?
在这个问题中,接受的答案使用以下语法:
typedef std::map<std::string, Base*(*)()> map_type;
Run Code Online (Sandbox Code Playgroud)
有人可以解释(*)的意思,我以前从未见过它吗?
我需要将(boost :: asio::) streambuf的内容复制到std :: string.
以下代码有效,但我认为_msg和临时std :: string之间存在不必要的副本:
Msg (boost::asio::streambuf & sb, size_t bytes_transferred) :
_nBytesInMsg (bytes_transferred)
{
boost::asio::streambuf::const_buffers_type buf = sb.data();
_msg = std::string(
boost::asio::buffers_begin(buf),
boost::asio::buffers_begin(buf) + _nBytesInMsg);
}
Run Code Online (Sandbox Code Playgroud)
我尝试用以下内容替换:
_msg.reserve(_nBytesInMsg);
std::copy(
boost::asio::buffers_begin(buf),
boost::asio::buffers_begin(buf) + _nBytesInMsg,
_msg.begin()
);
Run Code Online (Sandbox Code Playgroud)
在编译时,它不会将任何内容复制到_msg字符串.
编译器(gcc4.4.7)会优化这种情况 - 例如将streambuf直接复制到_msg而不使用临时的吗?
是否有一个迭代器,我可以使用boost :: asio :: streambuf :: const_buffers_type,以使std :: copy工作?
我有一行python通过回车符分割文件:
lines = open(sFile, 'r').read().split("0d".decode('hex'))
Run Code Online (Sandbox Code Playgroud)
这个文件是关闭的吗?如果没有,我可以以某种方式获取文件句柄吗?
我正在使用(最新版本)Cassandra nosql dbms来建模一些数据.
我想了解上个月活跃客户帐户的数量.
我创建了下表:
CREATE TABLE active_accounts
(
customer_name text,
account_name text,
date timestamp,
PRIMARY KEY ((customer_name, account_name))
);
Run Code Online (Sandbox Code Playgroud)
因为我想按日期过滤,我在日期列上创建了一个索引:
CREATE INDEX ON active_accounts (date);
Run Code Online (Sandbox Code Playgroud)
当我插入一些数据时,Cassandra会自动更新任何现有主键匹配的数据,因此以下插入只生成两条记录:
insert into active_accounts (customer_name, account_name, date) Values ('customer2', 'account2', 1418377413000);
insert into active_accounts (customer_name, account_name, date) Values ('customer1', 'account1', 1418377413000);
insert into active_accounts (customer_name, account_name, date) Values ('customer2', 'account2', 1418377414000);
insert into active_accounts (customer_name, account_name, date) Values ('customer2', 'account2', 1418377415000);
Run Code Online (Sandbox Code Playgroud)
这正是我想要的 - 我不会得到一个庞大的数据表,表中的每个条目代表一个独特的客户帐户 - 所以不需要选择不同的.
我想提出的问题是:上个月有多少不同的客户帐户处于活动状态:
Select count(*) from active_accounts where date >= …Run Code Online (Sandbox Code Playgroud) c++ ×6
c++11 ×3
boost ×2
boost-asio ×1
cassandra ×1
find ×1
linux ×1
python ×1
replace ×1
std ×1
std-function ×1
stringstream ×1
types ×1
vector ×1