Que dois-je faire pour que quand jeMultipliant une chaîne par un int en C++
string s = ".";
Si je
cout << s * 2;
Sera-ce la même chose que
cout << "..";
?
Que dois-je faire pour que quand jeMultipliant une chaîne par un int en C++
string s = ".";
Si je
cout << s * 2;
Sera-ce la même chose que
cout << "..";
?
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
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
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;
}
Si vous faites cela, vous voulez probablement aussi faire 'operator * (int, std :: string const &)'. –
@JamesKanze - bonne idée. – Ferruccio
Ceci est une mauvaise implémentation. L'utilisation d'un flux est inutile lorsque 'std :: string' a' append() 'et' operator + = ' – Bulletmagnet
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,
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
..
il ne sera même compiler – Andrew
Pourquoi ne pas vous faire un essai et voir pour vous-même? – chessweb
mieux si vous demandez comment faire cela fonctionne – janisz