C++ 编写包含变量值的多行字符串
我有一些程序参数,这些参数要求我有一个单一的格式化字符串,其中包含在程序过程中输入的变量值。由于涉及的数据量很大,因此每个新数据点的换行符都是理想的C++ 编写包含变量值的多行字符串,c++,string,visual-c++,c++17,C++,String,Visual C++,C++17,我有一些程序参数,这些参数要求我有一个单一的格式化字符串,其中包含在程序过程中输入的变量值。由于涉及的数据量很大,因此每个新数据点的换行符都是理想的 我使用VisualStudio的C++编译器,并已经有了以下的标题: //preprocessors #include <iostream> #include "MortCalc.h" #include <string> #include <istream> #include <ctime> #inc
我使用VisualStudio的C++编译器,并已经有了以下的标题:
//preprocessors
#include <iostream>
#include "MortCalc.h"
#include <string>
#include <istream>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <vector>
using namespace std;
但我经常遇到这样的错误:“表达式必须具有整数或非范围枚举类型”
我将此格式基于cout语句的工作方式,并替换了所有“在进行字符串连接时不能使用
setPrecision
和fixed
修饰符。
您可以使用std::stringstream
执行此操作,但是:
// In the header
#include <sstream>
// In your function
std::stringstream ss;
ss << " Principal Of Loan: $" << mortData.principal << '\n';
ss << " Interest Rate: " + mortData.interest + "%\n";
// more lines...
string mortgageInfo = ss.str();
//在标题中
#包括
//在你的职责范围内
std::stringstream-ss;
ss字符串文本(“…”)的类型为const char*,而不是std::String,它们的运算符+不是串联,而是添加到指向内存的地址
使用“…”来实际创建std::string文本(但std::fixed等仍然不起作用),或者创建一个
std::stringstream out;
然后使用操作符你所做的与你认为的略有偏差
代码行正在使用。。。不幸的是,它不允许整数或任何其他非字符串值在它里面…
但是,您有两个选择:
#include <sstream>
int main() {
std::stringstream some_stream;
some_stream << first_number << "ABC" << number << std::endl;
some_function_that_uses_only_strings(some_str.str());
}
从C++11使用
示例:(不干净!)
绝对不能向字符串中添加整数。。。使用…您应该使用字符串流
#include <string>
int main() {
some_function_that_uses_only_strings("ABC" + std::to_string(number));
}
#include <sstream>
int main() {
std::stringstream some_stream;
some_stream << first_number << "ABC" << number << std::endl;
some_function_that_uses_only_strings(some_str.str());
}