c++ - 用一个int乘以一个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11843226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiplying a string by an int in c++
提问by user1575615
What do I have to do so that when I
我该怎么做才能当我
string s = ".";
If I do
如果我做
cout << s * 2;
Will it be the same as
会不会和
cout << "..";
?
?
回答by JRG
std::string has a constructor of the form
std::string 具有以下形式的构造函数
std::string(size_type count, char c);
that will repeat the character. For example
这将重复该字符。例如
#include <iostream>
int main() {
std::string stuff(2, '.');
std::cout << stuff << std::endl;
return 0;
}
will output
会输出
..
回答by ForEveR
No, std::string
has no operator *
. You can add (char, string) to other string. Look at this http://en.cppreference.com/w/cpp/string/basic_string
不,std::string
没有operator *
。您可以将 (char, string) 添加到其他字符串。看看这个http://en.cppreference.com/w/cpp/string/basic_string
And if you want this behaviour (no advice this) you can use something like this
如果你想要这种行为(没有建议),你可以使用这样的东西
#include <iostream>
#include <string>
template<typename Char, typename Traits, typename Allocator>
std::basic_string<Char, Traits, Allocator> operator *
(const std::basic_string<Char, Traits, Allocator> s, size_t n)
{
std::basic_string<Char, Traits, Allocator> tmp = s;
for (size_t i = 0; i < n; ++i)
{
tmp += s;
}
return tmp;
}
template<typename Char, typename Traits, typename Allocator>
std::basic_string<Char, Traits, Allocator> operator *
(size_t n, const std::basic_string<Char, Traits, Allocator>& s)
{
return s * n;
}
int main()
{
std::string s = "a";
std::cout << s * 5 << std::endl;
std::cout << 5 * s << std::endl;
std::wstring ws = L"a";
std::wcout << ws * 5 << std::endl;
std::wcout << 5 * ws << std::endl;
}
http://liveworkspace.org/code/52f7877b88cd0fba4622fab885907313
http://liveworkspace.org/code/52f7877b88cd0fba4622fab885907313
回答by Ferruccio
There is no predefined *
operator that will multiply a string by an int
, but you can define your own:
没有*
将字符串乘以 的预定义运算符int
,但您可以定义自己的运算符:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string operator*(const string& s, unsigned int n) {
stringstream out;
while (n--)
out << s;
return out.str();
}
string operator*(unsigned int n, const string& s) { return s * n; }
int main(int, char **) {
string s = ".";
cout << s * 3 << endl;
cout << 3 * s << endl;
}
回答by huseyin tugrul buyukisik
Strings cannot be multiplied.
字符串不能相乘。
if s is a char
如果 s 是一个字符
'.' //this has 46 ascii-code
then
然后
cout << (char)((int)s * 2);
will give you
会给你
'/' //this has 92 ascii-code
回答by jcoder
They can't be multipled but I think you can write your own function to do this, something like -
它们不能相乘,但我认为您可以编写自己的函数来执行此操作,例如-
#include <iostream>
#include <string>
std::string operator*(std::string s, size_t count)
{
std::string ret;
for(size_t i = 0; i < count; ++i)
{
ret = ret + s;
}
return ret;
}
int main()
{
std::string data = "+";
std::cout << data * 10 << "\n";
}
It's probably not the best idea though, it will be very confusing to anyone looking at the code and not expecting this,
不过,这可能不是最好的主意,对于任何查看代码但不期望这一点的人来说,这将非常令人困惑,
回答by Sanjin Brus
You can do this:
你可以这样做:
#include <iostream>
using namespace std;
int main()
{
string text,new_text;
int multiply_number;
cin>>text>>multiply_number;
/*First time in for loop:new_text=new_text+text
new_text=""+"your text"
new_text="your text"
Secound time in for loop:new_text=new_text+text
new_text="your text"+"your text"
new_text="your textyour text"...n times */
for(int i=0;i<multiply_number;i++){
new_text+=text;
}
cout<<new_text<<endl; // endl="\n"
system ("pause");
return 0;
}
In python you can multiply string like this:
在python中,您可以像这样乘以字符串:
text="(Your text)"
print(text*200)