使用 iomanip 库的 C++ 操作
C++ manipulation using iomanip library
我是 C++ STL 库的新手,需要帮助。
我想添加两个数字假设 A = 4555 和 B = 50,并将它们输出为:
4555
+50
4605
另一个例子:
500000 + 12
500000
+12
500012
如果我将 A 和 B 都存储为整数数据类型,而将符号“+”存储为字符数据类型。我如何操纵它们以获得首选输出。
我只是不知道如何一起操作两个变量。
如果您可以使用恒定宽度 (或等于所涉及数字的最大宽度的可变宽度) 和 <iomanip>
中的 std::setw
作为:
#include <iostream>
#include <iomanip>
#include <string>
void display_sum(int a, int b)
{
std::cout << std::setw(10) << a << "\n"
<< std::setw(10) << ("+" + std::to_string(b)) << "\n"
<< std::setw(10) << (a+b) <<"\n" << std::endl;
}
int main()
{
display_sum(4555, 50);
display_sum(500000, 12);
display_sum(503930, 3922);
}
输出:
4555
+50
4605
500000
+12
500012
503930
+3922
507852
您可以使用操纵符 std::showpos、std::noshowpos 和 std::setw:
#include <iostream>
#include <iomanip>
int main() {
int a = 4555;
int b = 50;
std::cout
<< std::noshowpos << std::setw(10) << a << '\n'
<< std::showpos << std::setw(10) << b << '\n'
<< std::noshowpos << std::setw(10) << (a+b) << '\n';
}
如果您想要一个取决于值的宽度,您可以使用三个 std::ostringstream(s) 并创建中间字符串(没有 setw)。之后,您使用每个字符串的最大长度打印字符串 setw:
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
int main() {
int a = 4555;
int b = 50;
std::ostringstream as;
std::ostringstream bs;
std::ostringstream rs;
as << std::noshowpos << a;
bs << std::showpos << b;
rs << std::noshowpos << (a+b);
unsigned width = std::max({ as.str().size(), bs.str().size(), rs.str().size() });
std::cout
<< std::setw(width) << as.str() << '\n'
<< std::setw(width) << bs.str() << '\n'
<< std::setw(width) << rs.str() << '\n';
}
另请参阅:
注意:你可以看看操纵器std::internal。
在您的示例中,字段最多可以包含 7 个字符。也许您想在写入之前将字符串的大小调整为 7。例如fname.resize(7)
.
要根据需要格式化它,您需要 #include <iomanip>
并使用 std::left
和 std::setw(7)
。
file1 << left << setw(7) << fname
<< tab << setw(7) << lname
<< tab << setw(7) << street
<< tab << setw(7) << city
<< tab << setw(7) << state
<< tab << setw(7) << zip << endl;
我是 C++ STL 库的新手,需要帮助。
我想添加两个数字假设 A = 4555 和 B = 50,并将它们输出为:
4555
+50
4605
另一个例子:
500000 + 12
500000
+12
500012
如果我将 A 和 B 都存储为整数数据类型,而将符号“+”存储为字符数据类型。我如何操纵它们以获得首选输出。 我只是不知道如何一起操作两个变量。
如果您可以使用恒定宽度 (或等于所涉及数字的最大宽度的可变宽度) 和 <iomanip>
中的 std::setw
作为:
#include <iostream>
#include <iomanip>
#include <string>
void display_sum(int a, int b)
{
std::cout << std::setw(10) << a << "\n"
<< std::setw(10) << ("+" + std::to_string(b)) << "\n"
<< std::setw(10) << (a+b) <<"\n" << std::endl;
}
int main()
{
display_sum(4555, 50);
display_sum(500000, 12);
display_sum(503930, 3922);
}
输出:
4555
+50
4605
500000
+12
500012
503930
+3922
507852
您可以使用操纵符 std::showpos、std::noshowpos 和 std::setw:
#include <iostream>
#include <iomanip>
int main() {
int a = 4555;
int b = 50;
std::cout
<< std::noshowpos << std::setw(10) << a << '\n'
<< std::showpos << std::setw(10) << b << '\n'
<< std::noshowpos << std::setw(10) << (a+b) << '\n';
}
如果您想要一个取决于值的宽度,您可以使用三个 std::ostringstream(s) 并创建中间字符串(没有 setw)。之后,您使用每个字符串的最大长度打印字符串 setw:
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
int main() {
int a = 4555;
int b = 50;
std::ostringstream as;
std::ostringstream bs;
std::ostringstream rs;
as << std::noshowpos << a;
bs << std::showpos << b;
rs << std::noshowpos << (a+b);
unsigned width = std::max({ as.str().size(), bs.str().size(), rs.str().size() });
std::cout
<< std::setw(width) << as.str() << '\n'
<< std::setw(width) << bs.str() << '\n'
<< std::setw(width) << rs.str() << '\n';
}
另请参阅:
注意:你可以看看操纵器std::internal。
在您的示例中,字段最多可以包含 7 个字符。也许您想在写入之前将字符串的大小调整为 7。例如fname.resize(7)
.
要根据需要格式化它,您需要 #include <iomanip>
并使用 std::left
和 std::setw(7)
。
file1 << left << setw(7) << fname
<< tab << setw(7) << lname
<< tab << setw(7) << street
<< tab << setw(7) << city
<< tab << setw(7) << state
<< tab << setw(7) << zip << endl;