将C ++字符串拆分为多行(代码语法,不解析) [英] Splitting C++ Strings Onto Multiple Lines (Code Syntax, Not Parsing)

查看:128
本文介绍了将C ++字符串拆分为多行(代码语法,不解析)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不要混淆如何拆分字符串,例如:

http://stackoverflow.com/questions/236129/how-to-split-a-string

Not to be confused with how to split a string parsing wise, e.g.:
http://stackoverflow.com/questions/236129/how-to-split-a-string

我有点困惑如何在c ++中将一个字符串拆分成多行。

I am a bit confused as to how to split a string onto multiple lines in c++.

这听起来像一个简单的问题,但是采用下面的例子:

This sounds like a simple question, but take the following example:

#include <iostream>
#include <string>
main() {
  //Gives error
  std::string my_val ="Hello world, this is an overly long string to have" +
    " on just one line";
  std::cout << "My Val is : " << my_val << std::endl;

  //Gives error
  std::string my_val ="Hello world, this is an overly long string to have" &
    " on just one line";  
  std::cout << "My Val is : " << my_val << std::endl;
}

我意识到我可以使用 std ::字符串 append()方法,但我想知道是否有任何更短/更优雅(例如更多的pythonlike, 'c支持在c ++中)的方式将c ++中的字符串断开到多行,以便于阅读。

I realize that I could use the std::string append() method, but I was wondering if there was any shorter/more elegant (e.g. more pythonlike, though obviously triple quotes etc. aren't supported in c++) way to break strings in c++ onto multiple lines for sake of readability.

一个地方,这将是特别可取的是当你通过long字符串字面量到函数(例如句子)。

One place where this would be particularly desirable is when you're passing long string literals to a function (for example a sentence).

推荐答案

C ++ lexing阶段的一部分是将相邻的字符串文字(甚至通过换行符和注释)组合成单个文字。

Don't put anything between the strings. Part of the C++ lexing stage is to combine adjacent string literals (even over newlines and comments) into a single literal.

#include <iostream>
#include <string>
main() {
  std::string my_val ="Hello world, this is an overly long string to have" 
    " on just one line";
  std::cout << "My Val is : " << my_val << std::endl;
}

注意,如果你想在文字中使用换行符,自己:

Note that if you want a newline in the literal, you will have to add that yourself:

#include <iostream>
#include <string>
main() {
  std::string my_val ="This string gets displayed over\n" 
    "two lines when sent to cout.";
  std::cout << "My Val is : " << my_val << std::endl;
}

如果你想混合 #define d整数常量到文字中,你必须使用一些宏:

If you are wanting to mix a #defined integer constant into the literal, you'll have to use some macros:

#include <iostream>
using namespace std;

#define TWO 2
#define XSTRINGIFY(s) #s
#define STRINGIFY(s) XSTRINGIFY(s)

int main(int argc, char* argv[])
{
    std::cout << "abc"   // Outputs "abc2DEF"
        STRINGIFY(TWO)
        "DEF" << endl;
    std::cout << "abc"   // Outputs "abcTWODEF"
        XSTRINGIFY(TWO) 
        "DEF" << endl;
}

由于stringify处理器运算符的工作原理,您需要两个级别的宏,以获取 TWO 的实际值作为字符串文字。

There's some weirdness in there due to the way the stringify processor operator works, so you need two levels of macro to get the actual value of TWO to be made into a string literal.

这篇关于将C ++字符串拆分为多行(代码语法,不解析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆