我已经写了一些代码投const char*来int使用constexpr,因此我可以使用const char*作为一个模板参数.这是代码:
#include <iostream>
class conststr
{
public:
template<std::size_t N>
constexpr conststr(const char(&STR)[N])
:string(STR), size(N-1)
{}
constexpr conststr(const char* STR, std::size_t N)
:string(STR), size(N)
{}
constexpr char operator[](std::size_t n)
{
return n < size ? string[n] : 0;
}
constexpr std::size_t get_size()
{
return size;
}
constexpr const char* get_string()
{
return string;
}
//This method is related with Fowler–Noll–Vo hash function
constexpr unsigned hash(int n=0, unsigned h=2166136261)
{
return n …Run Code Online (Sandbox Code Playgroud) 我想跨多个c文件共享某些C字符串常量.常量跨越多行以便于阅读:
const char *QUERY = "SELECT a,b,c "
"FROM table...";
Run Code Online (Sandbox Code Playgroud)
上面的操作给出了QUERY的重新定义错误.我不想使用宏,因为每行后都需要退格'\'.我可以在单独的c文件中定义这些并在h文件中对变量进行extern但是我觉得这样做很懒.
有没有其他方法可以在C中实现这一目标?
class A {
public static void main(String[] args) {
System.out.println("\u2300");
System.out.println("\u10035");
}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过它写一条线(⌀)就好了,但十字符号没有出现,而只是打印数字5:
# javac A.java && java A
?
?5
Run Code Online (Sandbox Code Playgroud)
为什么?
考虑以下代码:
const char* someFun() {
// ... some stuff
return "Some text!!"
}
int main()
{
{ // Block: A
const char* retStr = someFun();
// use retStr
}
}
Run Code Online (Sandbox Code Playgroud)
在函数中someFun(),"Some text!!"存储的位置(我认为它可能在ROM的某个静态区域)以及它是什么范围 一生?
指向的内存是否会在retStr整个程序中被占用,或者在块A退出后被释放?
我想写一些带字符串文字的函数 - 只有一个字符串文字:
template <size_t N>
void foo(const char (&str)[N]);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这太过于扩展并且会匹配任何数组char- 无论它是否是真正的字符串文字.虽然在编译时无法区分它们之间的区别 - 而不必在运行时求助调用者包装文字/数组 - 但这两个数组将在内存中完全不同的位置:
foo("Hello"); // at 0x400f81
const char msg[] = {'1', '2', '3'};
foo(msg); // at 0x7fff3552767f
Run Code Online (Sandbox Code Playgroud)
有没有办法知道字符串数据在内存中的位置,以便我至少可以assert使该函数仅采用字符串文字?(使用gcc 4.7.3,但实际上任何编译器的解决方案都会很棒).
所以有点奇怪的问题我无法提出搜索条件.如果我有一个多行字符串在我的程序文字,反正是有让我的代码一致的缩进不会增加不必要的空白,以我的字符串文字?
例如:
if (true)
{
if (!false)
{
//Some indented code;
stringLiteral = string.format(
@"This is a really long string literal
I don't want it to have whitespace at
the beginning of each line, so I have
to break the indentation of my program
I also have vars here
{0}
{1}
{2}",
var1, var2, var3);
}
}
Run Code Online (Sandbox Code Playgroud)
它可能只是我的OCD说话,但无论如何都要保持我的程序的缩进而不向字符串添加不需要的空格,或者必须逐行构建它(真正的字符串是一个超长的string.format,即20~行里面有12个变量)?
下面的代码安全吗?编写类似于此的代码可能很诱人:
#include <map>
const std::map<const char*, int> m = {
{"text1", 1},
{"text2", 2}
};
int main () {
volatile const auto a = m.at("text1");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该映射仅用于字符串文字.
我认为它是完全合法的并且似乎正在工作,但是我从未看到保证在两个不同的地方使用的文字指针是相同的.我无法让编译器为具有相同内容的文字生成两个单独的指针,所以我开始怀疑这个假设是多么坚定.
我只对具有相同内容的文字是否可以有不同的指针感兴趣.或者更正式的,上面的代码可以除外吗?
我知道有一种编写代码的方法可以确保它有效,我认为上面的方法很危险,因为编译器可以决定为文字分配两个不同的存储,特别是如果它们放在不同的翻译单元中.我对吗?
这可能是一个简单的问题,但为什么 const char* 不需要指向的内存地址?
例子:
const char* a = "Anthony";
Run Code Online (Sandbox Code Playgroud)
并不是:
const char *a = // Address to const char
Run Code Online (Sandbox Code Playgroud)
像其他类型一样吗?
当std::views::split()获取未命名的字符串文字作为模式时,它不会分割字符串,但可以很好地处理未命名的字符文字。
#include <iomanip>
#include <iostream>
#include <ranges>
#include <string>
#include <string_view>
int main(void)
{
using namespace std::literals;
// returns the original string (not splitted)
auto splittedWords1 = std::views::split("one:.:two:.:three", ":.:");
for (const auto word : splittedWords1)
std::cout << std::quoted(std::string_view(word));
std::cout << std::endl;
// returns the splitted string
auto splittedWords2 = std::views::split("one:.:two:.:three", ":.:"sv);
for (const auto word : splittedWords2)
std::cout << std::quoted(std::string_view(word));
std::cout << std::endl;
// returns the splitted string
auto splittedWords3 = std::views::split("one:two:three", ':');
for (const auto word : …Run Code Online (Sandbox Code Playgroud) 我正在使用gnuplot在C++中绘制图形.该图正如预期的那样绘制,但在编译期间会出现警告.警告意味着什么?
warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的功能:
void plotgraph(double xvals[],double yvals[], int NUM_POINTS)
{
char * commandsForGnuplot[] = {"set title \"Probability Graph\"",
"plot 'data.temp' with lines"};
FILE * temp = fopen("data.temp", "w");
FILE * gnuplotPipe = popen ("gnuplot -persistent ", "w");
int i;
for (i=0; i < NUM_POINTS; i++)
{
fprintf(temp, "%lf %lf \n", xvals[i], yvals[i]);
//Write the data to a te mporary file
}
for (i=0; i < NUM_COMMANDS; i++)
{
fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); …Run Code Online (Sandbox Code Playgroud)