Вопрос

So, I want to know, how to do my code shorter:

  set<char> t;
  t.insert( 'a');
  t.insert( 'A');
  t.insert( 'o');
  t.insert( 'O');   
  t.insert( 'y');
  t.insert( 'Y');
  t.insert( 'e');
  t.insert( 'E');
  t.insert( 'u');
  t.insert( 'U');
  t.insert( 'i');
  t.insert( 'I');
  cout<<t.count('O');

I'm sure, thet is another way will be shorter and more correctly. What is the easiest way do you know?

Это было полезно?

Решение

With C++11 initialization:

std::set<char> t = {'a', <the rest>, 'I'};

Другие советы

#include <algorithm>
//...

std::set<char> t;
std::string s= "aAoOyYeEuUiI";

std::copy( s.begin() , s.end(), std::inserter(t, t.begin() ) ) ;
set<char> t;
char v[] = {'a', 'o', 'y', 'e', 'u', 'i'};
for (int i = 0;  i < 6; ++i )
{
   t.insert(v[i]);
   t.insert(v[i]+'A'-'a');
}

If Boost is an option - try the assign library.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top