Multiplying a string by an int in C++
67,427
Solution 1
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
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
Solution 2
std::string has a constructor of the form
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
..
Solution 3
I used operator overloading to simulate this behavior in c++.
#include <iostream>
#include <string>
using namespace std;
/* Overloading * operator */
string operator * (string a, unsigned int b) {
string output = "";
while (b--) {
output += a;
}
return output;
}
int main() {
string str = "abc";
cout << (str * 2);
return 0;
}
Output: abcabc
Solution 4
There is no predefined *
operator that will multiply a string by an int
, but you can define your own:
#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;
}
Solution 5
Strings cannot be multiplied.
If s is a char
'.' // This has ASCII code 46
then
cout << (char)((int)s * 2);
will give you
'/' // This has ASCII code 92
Author by
skittles sour
Updated on July 22, 2022Comments
-
skittles sour almost 2 years
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 << "..";
?
-
Andrew over 11 yearsit will not even compile
-
chessweb over 11 yearsWhy don't you give it a try and see for yourself?
-
janisz over 11 yearsBetter if you ask how to make it works
-
jcoder over 11 yearsEven though the answer is "no" I don;t see that this is worthy of so many downvotes. It's not a stupid question if you are used to other languages and not easily answered unless you want to read 1000+ pages of specs. It might have been better as "I tried this and it didn't work, so how can I achieve this" instead, but even so...
-
Bo Persson over 11 yearsOddly enough
cout << (s + s)
does work, so why notcout << (s * 2)
? No particular reason, it just isn't defined by the language.
-
-
James Kanze over 11 yearsIf you do this, you probably want to do
operator*(int, std::string const&)
as well. -
moooeeeep over 8 yearsShouldn't
size_t
rather betypename std::basic_string<Char, Traits, Allocator>::size_type
here? -
ForEveR over 8 years@moooeeeep should be, but it's not very important here.
-
Bulletmagnet over 6 yearsThis is a bad implementation. Using a stream is wasteful when
std::string
hasappend()
andoperator+=
-
greybeard about 5 yearsYou can, but even as the accepted answer implements string*int pretty much like this: DON'T. For starters, use a double and (conditionally) add approach.
-
shayst over 4 yearsPerfectly elegant
-
Peter Mortensen almost 4 yearsRe "
system("pause");
": That is operating system dependent. What is the intent? On Windows, it will likely be an infinite delay (until a key is pressed). On Linux it will likely be a slight delay before it gives up ("pause: command not found"). Is the intent to avoid a terminal window closing before the result can be seen? -
Peter almost 4 yearsTo avoid doing (or potentially doing) multiple reallocations, can use a simple
tmp.reserve(n*s.size())
or similar. I wouldn't advise writing anoperator*()
like this though - a helper function that accepts a string and a repeat count, and returns the result, would arguably be clearer.