使用打印时有没有办法对齐文字std::cout?我正在使用标签,但是当单词太大时,它们将不再对齐.
Sales Report for September 15, 2010
Artist Title Price Genre Disc Sale Tax Cash
Merle Blue 12.99 Country 4% 12.47 1.01 13.48
Richard Music 8.49 Classical 8% 7.81 0.66 8.47
Paula Shut 8.49 Classical 8% 7.81 0.72 8.49
Run Code Online (Sandbox Code Playgroud) 如何在C++中格式化输出?换句话说,什么是C++相当于使用printf这样的:
printf("%05d", zipCode);
Run Code Online (Sandbox Code Playgroud)
我知道我可以printf在C++中使用,但我更喜欢输出运算符<<.
你会使用以下内容吗?
std::cout << "ZIP code: " << sprintf("%05d", zipCode) << std::endl;
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习C,但我已经遇到了问题.我认为它是微不足道的,但我需要知道它.我已经写了:
#include <stdio.h>
#include <string.h>
int main()
{
char str_a[20];
strcpy(str_a, "Hello, world!\n");
printf(str_a);
}
Run Code Online (Sandbox Code Playgroud)
一旦我尝试使用以下命令编译它:gcc -g -o char_array2 char_array2.c我收到错误说:
char_array2.c: In function ‘main’:
char_array2.c:9:2: warning: format not a string literal and no format arguments [-Wformat-security]
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
我没有太多使用C++的经验.相反,我在C#中工作得更多,所以,我想问一下我在那里会做些什么.我必须生成一个特定的字符串格式,我必须传递给另一个函数.在C#中,我可以通过以下简单代码轻松生成字符串.
string a = "test";
string b = "text.txt";
string c = "text1.txt";
String.Format("{0} {1} > {2}", a, b, c);
Run Code Online (Sandbox Code Playgroud)
通过生成这样一个上面的字符串,我应该能够通过它system().但是,system只接受char*
我在Win32 C++(不是C++/CLI),并且不能使用,boost因为它包含太多包含项目本身非常小的所有文件.喜欢的东西,sprintf()在我看来是有用的,但sprintf不接受string的a,b和c参数.有什么建议我如何生成这些格式化的字符串以传递给我的程序中的系统?
char hello[] = "hello world";
std::string str;
str.resize(sizeof(hello)-1);
memcpy(&str[0], hello, sizeof(hello)-1);
Run Code Online (Sandbox Code Playgroud)
此代码是C++ 98中未定义的行为.它在C++ 11中是合法的吗?
我希望在比较期间控制double的精度,然后使用C++返回默认精度.
我打算用来setPrecision()设定精度.那么将精度设置回默认值的语法是什么(如果有的话)?
我正在做这样的事情
std::setPrecision(math.log10(m_FTOL));
Run Code Online (Sandbox Code Playgroud)
我做了一些事情,之后我想回到默认的双重比较.
我这样修改了,我还有一些错误
std::streamsize prec = std::ios_base::precision();
std::setprecision(cmath::log10(m_FTOL));
Run Code Online (Sandbox Code Playgroud)
cmath编译时为false,编译时为std::ios_basefalse.你能帮忙吗?
谢谢.
是否可以格式化std::string传递一组参数?
目前我正在以这种方式格式化字符串:
string helloString = "Hello %s and %s";
vector<string> tokens; //initialized vector of strings
const char* helloStringArr = helloString.c_str();
char output[1000];
sprintf_s(output, 1000, helloStringArr, tokens.at(0).c_str(), tokens.at(1).c_str());
Run Code Online (Sandbox Code Playgroud)
但是矢量的大小是在运行时确定的.是否有任何类似的函数sprintf_s采用参数集合并格式化std :: string/char*?我的开发环境是MS Visual C++ 2010 Express.
编辑: 我想实现类似的东西:
sprintf_s(output, 1000, helloStringArr, tokens);
Run Code Online (Sandbox Code Playgroud) 我正在通过visual studio 2008在win 7上运行一个程序
我收到此错误:
错误4错误C3861:'snprintf':找不到标识符
我已经包含了stdio标题...
我不知道还有什么可以遗漏
我sprintf在C++ 11中使用函数,方法如下:
std::string toString()
{
std::string output;
uint32_t strSize=512;
do
{
output.reserve(strSize);
int ret = sprintf(output.c_str(), "Type=%u Version=%u ContentType=%u contentFormatVersion=%u magic=%04x Seg=%u",
INDEX_RECORD_TYPE_SERIALIZATION_HEADER,
FORAMT_VERSION,
contentType,
contentFormatVersion,
magic,
segmentId);
strSize *= 2;
} while (ret < 0);
return output;
}
Run Code Online (Sandbox Code Playgroud)
有没有比这更好的方法来检查每次保留的空间是否足够?为了将来添加更多东西的可能性.