2012-08-07 1 views

Répondre

8

Non, std::string n'a pas operator *. Vous pouvez ajouter (char, string) à une autre chaîne. Regardez ce http://en.cppreference.com/w/cpp/string/basic_string

Et si vous voulez que ce comportement (pas de conseil cela), vous pouvez utiliser quelque chose comme ça

#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

+0

Ne devrait pas 'size_t' plutôt' typename std :: basic_string :: size_type' ici? – moooeeeep

+0

@moooeeeep devrait l'être, mais ce n'est pas très important ici. – ForEveR

1

Les chaînes ne peuvent pas être multipliés.

si s est char

'.'  //this has 46 ascii-code 

puis

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

vous donnera

'/'  //this has 92 ascii-code 
3

Il n'y a aucun opérateur * prédéfini qui multipliera une chaîne par un int, mais vous pouvez définir votre propre:

#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; 
} 
+0

Si vous faites cela, vous voulez probablement aussi faire 'operator * (int, std :: string const &)'. –

+0

@JamesKanze - bonne idée. – Ferruccio

+0

Ceci est une mauvaise implémentation. L'utilisation d'un flux est inutile lorsque 'std :: string' a' append() 'et' operator + = ' – Bulletmagnet

0

Ils ne peuvent pas être multiplée mais je pense que vous pouvez écrire votre propre fonction pour ce faire, quelque chose comme -

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

Il est sans doute pas la meilleure idée cependant, il sera très confus à tout le monde regardant le code et ne s'y attendait pas,

12

std :: string a un constructeur de la forme

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

qui répétera le caractère. Par exemple

#include <iostream> 

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

volonté sortie

..