c ++查找字符串
Program 1:
程序1:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << 3.123456789 << endl;
cout << setprecision(3) << 3.123456789 << endl;
return 0;
}
Output:
输出:
3.12346
3.12
Explanation:
说明:
Here, we used setprecision() manipulator then it sets the precision value. Here we used precision value 3, and then we print "3.12" instead of "3.123456789".
在这里,我们使用了setprecision()操纵器,然后它设置了精度值。 在这里,我们使用精度值3 ,然后打印“ 3.12”而不是“ 3.123456789” 。
Program 2:
程式2:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
unsigned char str[] = "World";
cout << "Hello" << setw(7) << str;
return 0;
}
Output:
输出:
Hello World
Explanation:
说明:
Here, we used setw() manipulator which is used to set the width of the string. If the specified string has less character than setw(), then it will add spaces at the left side of the string.
在这里,我们使用了setw()操纵器,该操纵器用于设置字符串的宽度。 如果指定的字符串的字符少于setw() ,则它将在字符串的左侧添加空格。
In our example, size of the str is 5 and we used 7 in setw() then it will add 2 spaces on the left side.
在我们的示例中, str的大小为5,我们在setw()中使用了7,然后它将在左侧添加2个空格。
Program 3:
程式3:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
unsigned char str[] = "World";
cout << "Hello" << setw(7) << str << str;
return 0;
}
Output:
输出:
Hello WorldWorld
Explanation:
说明:
Here, we used setw() manipulator which is used to set the width of the string. If the specified string has less character than setw(), then it will add spaces at the left side of the string.
在这里,我们使用了setw()操纵器,该操纵器用于设置字符串的宽度。 如果指定的字符串的字符少于setw() ,则它将在字符串的左侧添加空格。
Here, width is set only for 1st str in the cout statement. The 2nd str will be printed as it is. Then the final output "Hello WorldWorld" will be printed on the console screen.
在这里,宽度仅在COUT声明1 日 海峡设置。 2nd str将按原样打印。 然后,最终输出“ Hello WorldWorld”将被打印在控制台屏幕上。
Program 4:
计划4:
#include <iostream>
#include <iomanip>
#include <ios>
using namespace std;
int main()
{
cout << setw(15) << setfill('*') << "includehelp" << setw(20) << setfill('#') << "tutorials" << endl;
return 0;
}
Output:
输出:
****includehelp###########tutorials
Explanation:
说明:
Here, we used setw() and setfill() manipulators.
在这里,我们使用了setw()和setfill()操纵器。
setw() : This is used to set the width of string that will be printed on the console screen.
setw() :用于设置将在控制台屏幕上打印的字符串的宽度。
setfill() : This is used to fill the specified character.
setfill() :用于填充指定的字符。
We set width 15 for string "includehelp" so 4 '*' are padded at the left side. And set 20 for string "tutorials" so 11 '#' is padded at the left side.
我们为字符串“ includehelp”设置了宽度15,因此在左侧填充了4个“ *” 。 并为字符串“ tutorials”设置20,以便在左侧填充11个“#” 。
翻译自: https://www.includehelp.com/cpp-tutorial/manipulators-find-output-programs-set-2.aspx
c ++查找字符串