小编TMS*_*TMS的帖子

Python:如何修改/编辑打印到屏幕的字符串并将其读回?

我想在Windows中将字符串打印到命令行/终端,然后编辑/更改字符串并将其读回.谁知道怎么做?谢谢

print "Hell"
Hello!  <---Edit it on the screen
s = raw_input()
print s
Hello!
Run Code Online (Sandbox Code Playgroud)

python

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

如何在编译时将文本文件作为字符串包含在内,而不在文本文件中添加 c++11 字符串文字前缀和后缀

我知道这个网站上有很多类似的问题。我真的很喜欢以下链接中提到的解决方案:

\n\n

/sf/answers/1751506431/

\n\n

经过一些修改,您可以在编译时包含文本文件,例如:

\n\n
constexpr const char* s = \n#include "file.txt"\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是要使其工作,您必须向原始文件添加字符串文字前缀和后缀,例如

\n\n
R"(\nThis is the original content,\nand I don\'t want this file to be modified. but i\n don\'t know how to do it.\n)";\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的问题是:有没有一种方法可以使其工作但不修改 file.txt?

\n\n

(我知道我可以使用命令行工具来制作副本,在副本前面添加和附加到副本,在编译后删除副本。我正在寻找比这更优雅的解决方案。希望不需要其他工具)

\n\n

这是我尝试过的(但不起作用):

\n\n
#include <iostream>\n\nint main() {\n  constexpr const char* s =\n#include "bra.txt"  // R"(\n#include "file.txt" //original file without R"( and )";\n#include "ket.txt"  // )";\n  std::cout << s << "\\n";\n  return 0;\n}\n\n/opt/gcc8/bin/g++ -std=c++1z a.cpp\nIn file included from a.cpp:5:\nbra.txt:1:1: …
Run Code Online (Sandbox Code Playgroud)

metaprogramming c++11

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

如何使宏在for循环中生成代码?

我在代码审查中注意到以下代码:

for (int i = 0; i < 50; ++i) {
  switch(i) {
    case 0:
      return f1();
    case 1:
      return f2();
    ...
    case 49:
      return f50();
  }
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有任何优雅的方式,例如使用宏来简化此代码(将其减少到几行):

谢谢.

c c++ macros c-preprocessor

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

标签 统计

c ×1

c++ ×1

c++11 ×1

c-preprocessor ×1

macros ×1

metaprogramming ×1

python ×1