user3623421
user3623421

Reputation: 33

Add to set in C++

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?

Upvotes: 1

Views: 2212

Answers (4)

user1641854
user1641854

Reputation:

If Boost is an option - try the assign library.

Upvotes: -1

R Sahu
R Sahu

Reputation: 206607

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');
}

Upvotes: 0

P0W
P0W

Reputation: 47794

#include <algorithm>
//...

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

std::copy( s.begin() , s.end(), std::inserter(t, t.begin() ) ) ;

Upvotes: 2

davidhigh
davidhigh

Reputation: 15488

With C++11 initialization:

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

Upvotes: 6

Related Questions