我要求boost::filesystem::file_size一个带路径的"/tmp/test\ file.txt"文件,它在那里报告没有这样的文件或目录.
Error: fs::file_size("/tmp/test\ file.txt") reported boost::filesystem::file_size: No such file or directory
-rw-rw-r-- 1 rturrado users 12486 Mar 15 12:01 /tmp/test file.txt
Run Code Online (Sandbox Code Playgroud)
在boost :: filesystem中使用带有转义序列的路径时是否有任何注意事项?
我在Visio 2010中为UML图添加了大约50行文本的注释,我无法调整形状大小.我可以看到连接点,但不能看到可以拖动以调整形状大小的蓝色连接点.通过"视图"菜单,"任务窗格","大小和位置"手动修改高度也不起作用.
我只是为模式点击一些Xliff文件approved="no".我有一个Shell脚本和一个Python脚本,性能差异很大(对于一组393个文件,总共3,686,329行,Shell脚本的用户时间为0.1s,Python脚本为6.6s).
Shell:grep 'approved="no"' FILE
Python:
def grep(pattern, file_path):
ret = False
with codecs.open(file_path, "r", encoding="utf-8") as f:
while 1 and not ret:
lines = f.readlines(100000)
if not lines:
break
for line in lines:
if re.search(pattern, line):
ret = True
break
return ret
Run Code Online (Sandbox Code Playgroud)
有关使用多平台解决方案提高性能的任何想法吗?
在应用一些建议的解决方案后,这里有几个结果.
测试在RHEL6 Linux机器上运行,使用Python 2.6.6.
工作集:393个Xliff文件,总计3,686,329行.
数字是用户时间,以秒为单位.
grep_1(io,加入100,000个文件行):50s
grep_3(mmap):0.7s
Shell版本(Linux grep):0.130s
前几天我解决了另一个问题,涉及一个std::vector <std::pair<int,int>>被调用的name.
我的问题是,如何访问这种类型的name.first和name.second?
我最终使用了一个 ranged-for 循环,这解决了我的问题
for(i : name) { i->first , i->second}
Run Code Online (Sandbox Code Playgroud)
但是,还有别的办法吗?我对如何在正常for循环中访问它特别感兴趣,例如
for(int i = 0; i < name.size(); i++) { std::vector::std::pair::name.first}
Run Code Online (Sandbox Code Playgroud)
有人可以为我解释一下吗?
我有一个Foo带有私有成员的类x,并且我希望能够打印Foovia的实例fmt::print。
#include <fmt/format.h>
class Foo {
public:
explicit Foo(int i) : x{i} {}
private:
int x{};
};
template<>
struct fmt::formatter<Foo> {
template<typename ParseContext>
constexpr auto parse(ParseContext& ctx) {
return ctx.begin();
}
template<typename FormatContext>
auto format(const Foo& foo, FormatContext& ctx) {
return fmt::format_to(ctx.out(), "{}", foo.x);
}
};
int main() {
Foo foo{5};
fmt::print("{}", foo);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码存在尝试访问私有成员的问题Foo::x。
<source>:19:52: error: 'int Foo::x' is private within this context
19 | return fmt::format_to(ctx.out(), "{}", …Run Code Online (Sandbox Code Playgroud) 假设我们有一个简单的概念,例如:
template <typename T>
concept MyConcept = requires {
T::value == 42;
};
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这个概念是说,如果代码T::value == 42对于类型 T 有效,我就会通过。所以value必须是静态成员,对吗?
我有一个struct
struct Test { int value = 0; }
Run Code Online (Sandbox Code Playgroud)
和下一个模板函数
template <MyConcept T>
void call() { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时:
int main()
{
call<Test>();
}
Run Code Online (Sandbox Code Playgroud)
有用!
问题是:它为什么有效? Test::value == 42不是该类型的有效代码Test。
我找到了一种修复它的方法,例如:
template <typename T>
concept MyConcept = requires {
*(&T::value) == 42;
};
Run Code Online (Sandbox Code Playgroud)
它按预期“工作”:
<source>:6:20: note: the required expression '((* & T::value) == 42)' is …Run Code Online (Sandbox Code Playgroud) 我一直在玩下面这段代码.file_string返回一个临时字符串,该字符串只能"生效"直到语句结束.在Visual Studio 2008中,当您使用时pTempFolder,它会按预期包含垃圾.但是在Linux中,使用英特尔编译器11.0 pTempFolder仍然指向一个有效的字符串.编译器是否有关于临时销毁的不同政策,那种渴望(视觉)与懒惰(英特尔)?或许这只是一个巧合?
boost::filesystem wpathTempFolder("/tmp");
const wchar_t* const pTempFolder = wpathTempFolder.file_string().c_str();
// use pTempFolder
Run Code Online (Sandbox Code Playgroud)
顺便说一下,这是boost文件系统版本2.我也看到file_string在升级文件系统版本3 中已经被弃用.并且有一种新c_str方法可以在字符串上运行,而不是在临时字符串上运行.
/*filesystem 2*/
const string_type file_string() const;
/*filesystem 3*/
const string_type& native() const; // native format, encoding
const value_type* c_str() const; // native().c_str()
Run Code Online (Sandbox Code Playgroud) 我正在研究Project Euler的问题14(http://projecteuler.net/problem=14).我正在尝试使用memoization,以便将给定数字的序列长度保存为部分结果.我正在使用Data.MemoCombinators.下面的程序产生堆栈溢出.
import qualified Data.MemoCombinators as Memo
sL n = seqLength n 1
seqLength = Memo.integral seqLength'
where seqLength' n sum = if (n == 1) then sum
else if (odd n) then seqLength (3*n+1) (sum+1)
else seqLength (n `div` 2) (sum+1)
p14 = snd $ maximum $ zip (map sL numbers) numbers
where numbers = [1..max]
max = 999999
Run Code Online (Sandbox Code Playgroud)
堆栈溢出应该是由于sum+1被懒惰地评估.如何在每次调用之前强制对其进行评估seqLength?顺便说一句,备忘录是否实施得很好?我更有兴趣指出我的Haskell错误,而不是解决这个问题.
我正在尝试使用概念来检测给定类型的开始函数是否将迭代器返回到“const T&”或仅返回到“T&”。但我不确定如何最好地去做,我尝试了以下方法:
#include <iostream>
#include <concepts>
#include <vector>
#include <set>
template <typename T>
concept IsConst = std::is_const<T>::value;
template<typename T>
concept IsIterableOfConst = requires (T& t) { { *t.begin() } -> IsConst; };
template <IsIterableOfConst T>
void test()
{
std::cout << "Yes" << std::endl;
}
template <typename T>
void test()
{
std::cout << "No" << std::endl;
}
int main(int argc, char** argv)
{
test<std::set<int>>();
test<std::vector<int>>();
}
Run Code Online (Sandbox Code Playgroud)
这会产生输出“No No”,当我希望它产生输出“Yes No”时,因为 std::set 中的迭代器应该,据我所知,总是迭代持有类型的集合的“const”版本. 如何正确检测容器包含 const 值?
我有一些模板化类类型,例如 A、B、C,如下所示:
template < typename T >
class A{};
template < typename T >
class B{};
template < typename T >
class C{};
Run Code Online (Sandbox Code Playgroud)
现在我想要一个函数,它通常接受任何类型,例如:
template < typename T>
void Func()
{
std::cout << "Default " << __PRETTY_FUNCTION__ << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
现在我想将该函数专门化为仅接受给定的模板类之一,例如:
template < typename T>
void Func<A<T>>()
{
std::cout << "All A Types " << __PRETTY_FUNCTION__ << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这是不允许的,因为它只是部分专业化。好的。
我认为concept可能有帮助,但感觉我想得太复杂了。我的解决方案是:
template < typename T, template <typename > typename OUTER >
bool Check;
template < typename INNER, template …Run Code Online (Sandbox Code Playgroud) 只是想知道转发函数模板参数的确切影响(和优点)是什么,即:
template <class F>
void foo(F &&f) {
f(1); // how does this call
std::forward<F>(f)(1); // differ from this one?
}
Run Code Online (Sandbox Code Playgroud) 我认为an 的枚举enum [class]数是文字,因为根据我的理解,它们代表了代表 for 、for和for 的enum [class]东西。然而,它们不是,因为标准只列出了这些文字,1inttruebool"hello"char const*
literal:
integer-literal
character-literal
floating-point-literal
string-literal
boolean-literal
pointer-literal
user-defined-literal
Run Code Online (Sandbox Code Playgroud)
而字面量一词根本不会出现在[dccl.enum]中。
标准不将枚举数分类为其所属枚举类型的文字是否有原因?
我想补充一下,你知道将来是否有删除它的意图吗?
是不是因为当您编写低级代码(例如,可能混合使用 C++ 和汇编程序)时,它不是一个妖魔化的语句?