15

我该怎么做,这样当我

string s = ".";

如果我做

cout << s * 2;

会不会一样

cout << "..";

?

4

8 回答 8

56

std::string 具有形式为的构造函数

std::string(size_type count, char c);

这将重复字符。例如

#include <iostream>

int main() {
   std::string stuff(2, '.');
   std::cout << stuff << std::endl;
   return 0;
}

将输出

..
于 2012-08-07T11:18:43.403 回答
16

不,std::string没有operator *。您可以将 (char, string) 添加到其他字符串。看看这个http://en.cppreference.com/w/cpp/string/basic_string

如果你想要这种行为(没有建议)你可以使用这样的东西

#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

于 2012-08-07T09:45:13.630 回答
15

我在 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;
}

输出:abcabc

于 2020-06-22T18:15:21.983 回答
5

没有预定义*的运算符可以将字符串乘以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;
}
于 2012-08-07T11:09:24.213 回答
2

字符串不能相乘。

如果 s 是一个字符

'.'     // This has ASCII code 46

然后

cout << (char)((int)s * 2);

会给你

'/'     // This has ASCII code 92
于 2012-08-07T09:48:43.857 回答
2

它们不能被倍增,但我认为您可以编写自己的函数来执行此操作,例如-

#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";
}

不过,这可能不是最好的主意,对于任何查看代码并且没有预料到这一点的人来说,这将是非常令人困惑的,

于 2012-08-07T11:09:53.823 回答
0

你可以这样做:

#include <iostream>
using namespace std;

int main()
{
    string text, new_text;
    int multiply_number;

    cin >> text >> multiply_number;

    /*
        First time in the 'for' loop:  new_text = new_text + text
                                       new_text = "" + "your text"
                                       new_text = "your text"

        Second time in the '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;
}

在 Python 中,您可以像这样乘以字符串:

text = "(Your text)"
print(text*200)
于 2019-02-17T00:09:53.477 回答
0
std::string StrMultiply(const char* str, size_t count) {
        size_t stringsize = strlen(str);
        size_t buffersize = stringsize * count + 1;
        string res(buffersize,'\0');
        char* end = res._Unchecked_end();
        char* offset = res._Unchecked_begin();
        for (size_t i = 0;i < count; offset += stringsize,i++)
        {
            memcpy(offset, str, stringsize);
        }
        // mark the end
        res[buffersize - 1] = '\0';
        return res;
    }
        inline std::string operator*(std::string left, size_t right) {
            return StrMultiply(left.c_str(), right);
        }

这是一个 ram 友好的解决方案,比使用 stringstreams 或 string::append 快 10 倍

于 2021-08-18T15:18:14.603 回答