昨天,我在团队的代码中使用 jsoncpp 库遇到了这样的函数:
#include <json/value.h>
Json::Value makeList() {
Json::Value list(Json::arrayValue);
list.append(new Json::Value(true));
return list;
}
Run Code Online (Sandbox Code Playgroud)
它引起了我的注意,因为new在调用中使用list.append泄漏了一些内存,这可以通过简单地删除来修复new。然而,在调试过程中,我意识到Json::Value::append需要aconst Json::Value&或aJson::Value&&。C++ 不会将指针隐式转换为引用,因此 aJson::Value*不应隐式转换为其中任何一个。
为了证实这一点,我写了自己的简短示例:
#include <iostream>
class Foo {
public:
int x = 4;
};
void printValue(const Foo& f) { std::cout << "The value is " << f.x << ".\n"; }
void printValue(Foo&& f) { std::cout << "The value is " << f.x << ".\n"; } …Run Code Online (Sandbox Code Playgroud) 我有以下向量
std::vector<std::pair<std::string, std::function<std::string(const PlayerFRDb&)>>> stringExtractors = {
{" TEAM ", extractTeam},
{" NAME ", extractName},
{" TYPE ", extractProperty4},
};
std::vector<std::pair<std::string, std::function<int(const PlayerFRDb&)>>> intExtractors = {
{" WS ", extractProperty0},
{" LS ", extractProperty1},
{" W ", extractProperty2},
{" L ", extractProperty3},
};
std::vector<std::pair<std::string, std::function<char(const PlayerFRDb&)>>> charExtractors = {
{" WAY ", extractProperty5},
};
std::vector<std::pair<std::string, std::function<double(const PlayerFRDb&)>>> doubleExtractors = {
{" LINE ", extractProperty6},
};
Run Code Online (Sandbox Code Playgroud)
为了更容易地使用这些函数,我想将它们集中到一个向量中,但问题是它们返回不同的类型。
我尝试使用 std::variant,但无法初始化向量,因为编译器打印出:“没有构造函数的实例与参数列表参数类型匹配 ({...}, {...}, {. ..}、{...}、{...}、{...}、{...}、{...}) "
using FuncVariant = std::variant<
std::function<std::string(const PlayerFRDb&)>, …Run Code Online (Sandbox Code Playgroud) GetbufferSetLength(some_size)我想知道和之间到底有什么区别GetBuffer(some_size)。
该文档对我来说不是很清楚,而且对我来说,这两个函数听起来几乎都做了或多或少相同的事情。
这是我的用例:
CStringA foo;
char *p = foo.GetBufferSetLength(somelength);
// fill the buffer p with exactly somelength (non null) chars
FillCharBufferWithStuff(p, somelength);
foo.ReleaseBuffer(somelength); // or should I rather use
// ReleaseBufferSetLength(somelength) ?
// now foo contains the somelength characters provided by FillCharBufferWithStuff
Run Code Online (Sandbox Code Playgroud)
如果我使用它GetBuffer来代替GetBufferSetLength它,则可以完成完全相同的工作。
如果有人能阐明这一点,我将不胜感激。
当 C++ 创建std::ofstream它时,它立即并隐式创建底层文件。
我完全同意这种行为,除非我有一个代码,只有在运行期间才能看到是否会产生任何数据。
因此,我想避免在没有数据发送给空文件时创建空文件(事务完整性:没有数据,文件系统上没有更改)。
我看到两种我不太喜欢的方法:
tellg()),如果流为空,则删除该文件。我不喜欢创建和删除文件(有时文件很多)并且remove操作本身承担了太多责任。std::stringstream,收集输出并std::ofstream仅在字符串流不为空的情况下创建和复制内容。好多了,但仍然需要临时内存分配,这可能很大。对此有更好的解决方案吗?我是否缺少一些想法?
以代码的形式:
#include <fstream>
int main()
{
std::ofstream ofs("file.txt");
// Some code that might or might not output to ofs
// Some other code that might or might not output to ofs
// Some more code that might or might not output to ofs
// It would be nice if file is not created if no data sent to ofs
} …Run Code Online (Sandbox Code Playgroud) 我有一些管理 unique_ptrs 队列的代码。在其主循环中,它从队列中弹出一个条目,对其执行某些操作,然后允许该元素超出范围并被删除。到目前为止,没有问题。
我想添加一个功能,我们可以将其移动到其他地方,而不是必须删除该元素。问题是只有元素本身知道是否执行此操作或它需要去哪里。
主循环类似于:
std::deque< std::unique_ptr<Object> > queue;
while( !queue.empty() ) {
auto element = std::move( queue.front() );
queue.pop_front();
element->do_something();
element->recycle( std::move(element) ); // ?
}
Run Code Online (Sandbox Code Playgroud)
如果回收不是对象的方法,这将不是问题,但因为它是对象的方法,我不确定它应该是什么样子。通过上面的代码,我会得到类似的东西:
class Object {
public:
virtual void do_something();
virtual void recycle( std::unique_ptr<Object> unique_this ) {
// move unique_this somewhere, or don't and allow it to be deleted
}
};
Run Code Online (Sandbox Code Playgroud)
这安全吗?如果recycle不移动unique_this,它将在它自己的方法之一中删除它(不过在方法的末尾)。那是问题吗?
我想到避免删除这个的一种可能性是让recycle通过左值引用获取unique_ptr,我意识到这有点奇怪。这个想法是,如果它愿意,它会将其移动到某个地方,但如果不这样做,则直到回收返回后该对象才会被删除。不过,它不一定避免在其方法之一中可能删除对象的问题,因为我们必须确保无论我们将其移动到何处都不会立即删除它。
如果这样做不安全,后备计划是让 Object 返回一个回收器(即,一个不是执行回收的 Object 方法的可调用对象)。我认为这应该是可能的,但是考虑到不同的对象想要使用不同的回收器,管理回收器的类型和生命周期会有点混乱。
在我看来,第一个想法(按值传递 unqiue_ptr)是最好的,除非它违反任何规则。这样安全吗?有一个更好的方法吗?
我想查找数据集A是否在数据集B中。也就是说A中的所有记录都应该在B中。R中有没有办法检查这一点?
对于数据A
x1 x2 x3
1 2.3 a
3 1.5 b
Run Code Online (Sandbox Code Playgroud)
对于数据B
x1 x2 x3
0 0.5 a
1 2.3 a
3 1.5 b
2 2 b
Run Code Online (Sandbox Code Playgroud) 这对我来说似乎很疯狂:
use v6.d;
say "Before division by zero!";
my $result = divide(3, 0);
say "After division by zero!";
sub divide($a, $b) {
my $result;
$result = $a / $b; # Line 11
return $result;
}
sub go($x) {
say "hello";
say $x; # Line 17
}
go($result); # Line 20
--output:--
Before division by zero!
After division by zero!
hello
Attempt to divide by zero when coercing Rational to Str
in sub go at b.raku line 17
in block <unit> …Run Code Online (Sandbox Code Playgroud) 我有一个包含数千行的 1 列数据框,全部基于相同的模式构建,例如:
ids <- c("ETC|HMPI01000001|HMPI01000001.1 TAG: Genus Species, T05X3Ml2_CL10007Cordes1_1","ETC|HMPI31000002|HMPI31000002.1 TAG: Genus Species, T3X3Ml2_CL10157Cordes1_1", "ETC|HMPI01000007|HMPI01000007.1 TAG: Genus Species, T1X3Ml2_CL11231Cordes1_1")
df <- as.data.frame(ids)
Run Code Online (Sandbox Code Playgroud)
> df
ids
1 ETC|HMPI01000001|HMPI01000001.1 TAG: Genus Species, T05X3Ml2_CL10007Cordes1_1
2 ETC|HMPI31000002|HMPI31000002.1 TAG: Genus Species, T3X3Ml2_CL10157Cordes1_1
3 ETC|HMPI01000007|HMPI01000007.1 TAG: Genus Species, T1X3Ml2_CL11231Cordes1_1
Run Code Online (Sandbox Code Playgroud)
我想将这些字符分成两列: var1 和 var2 这样,并保留第二个管道之后和第一个空格之前的文本,以及空格之后第二个 T 中的文本。这些将是所有线路的共同模式。预期结果是:
> df
var1 var2
1 HMPI01000001.1 T05X3Ml2_CL10007Cordes1_1
2 HMPI31000002.1 T3X3Ml2_CL10157Cordes1_1
3 HMPI01000007.1 T1X3Ml2_CL11231Cordes1_1
Run Code Online (Sandbox Code Playgroud)
我尝试了几个受这里、那里或那里启发的正则表达式..但我无法弄清楚。
我目前有这个,但它没有给出预期的结果:
df2 <- df %>% separate(col = "ids", into = …Run Code Online (Sandbox Code Playgroud) 极坐标中的pandas.DataFrame.query相当于什么?
import pandas as pd
data= {
'A':["Polars","Python","Pandas"],
'B' :[23000,24000,26000],
'C':['30days', '40days',np.nan],
}
df = pd.DataFrame(data)
A B C
0 Polars 23000 30days
1 Python 24000 40days
2 Pandas 26000 NaN
Run Code Online (Sandbox Code Playgroud)
现在,定义一个变量item
item=24000
df.query("B>= @item")
A B C
1 Python 24000 40days
2 Pandas 26000 NaN
Run Code Online (Sandbox Code Playgroud)
现在,使用极坐标:
import polars as pl
df = pl.DataFrame(data)
item=24000
df.query("B>= @item")
Run Code Online (Sandbox Code Playgroud)
我得到:
AttributeError: 'DataFrame' object has no attribute 'query'
Run Code Online (Sandbox Code Playgroud)
我的大胆猜测是,df.filter()但语法看起来不一样,而且看起来也filter相当于?df.loc[]
假设我有以下代码
module A where
x :: Int
x = 5
module Main where
import A
import Lib
main :: IO ()
main = print (x + y)
Run Code Online (Sandbox Code Playgroud)
以及在外部库中
module Lib where
y :: Int
y = 10
Run Code Online (Sandbox Code Playgroud)
一切都很好,该库的版本为 v0.1.0.0,我将其包含在边界中mylib == 0.1.*
现在,该名称x :: Int已添加到 MyLib 的导出列表中,并且新版本已发布。它的版本是 v0.1.1.0,如PVP 流程图所示:
在这种情况下,只有[...]函数[...]被添加到库的导出接口中。不会导致破损 [...]
这个说法如何正确呢?当然我的代码不再编译。因为它不知道x从哪里拉。
c++ ×5
dataframe ×2
r ×2
haskell ×1
jsoncpp ×1
mfc ×1
performance ×1
python ×1
python-3.x ×1
raku ×1
regex ×1
std ×1
std-variant ×1
unique-ptr ×1
vector ×1
versioning ×1