Ignoring capital/lowercase letters using STL?

Hello -

I wanted to build a program that will check a user entry against a preset list of words and phrases to see if it exists (and if not, add it). I want to use STL containers and algorithms to do this, and so I started to use a vector<string> to hold the entries, however I can't figure out how to get around the capital and lowercase letters. I was planning on converting all of the entries and the user entry to either all caps or lowercase in an iteration, in the background, but can't figure it out.

1. Would this be the simplest/most efficient method?

2. How would you convert a whole vector to all caps or all lower case using STL algorithms?

Thanks
Ben
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>

void print( const std::vector<char>& v )
{
    for ( auto i : v )
        std::cout << i << ' ' ;
    std::cout << '\n' ;
}

int main()
{
    char buffer[] = "asaASDFSDFasdfasdfASDFasdf" ;

    std::vector<char> v (buffer, buffer+sizeof(buffer)) ;;
    print(v) ;
    
    std::transform(v.begin(), v.end(), v.begin(), std::tolower ) ;
    print(v) ;
}


Efficiency? It should be fairly efficient. Feel free to measure the performance.
Last edited on
Topic archived. No new replies allowed.