Search code examples
c++case-insensitiveistream

Overloading operator>> for case insensitive string


Given the definition of ci_string from cpp.reference.com, how would we go about implementing operator>>? My attempts at it involved std::read, but it doesn't seem to work (that is, gcount() properly counts the number of characters entered, but there is no output)

#include <iostream> 
#include <cctype> 
#include <string> 

// ci_string definition goes here 

std::istream& operator>>(std::istream& in, ci_string& str)
{
    return in.read(&*str.begin(), 4); 
} 

int main()
{
    ci_string test_str; 
    std::cin >> test_str; 
    std::cout << test_str; 
    return 0; 
} 

Solution

  • How about

    std::istream& operator>>(std::istream& in, ci_string& str)
    {
        std::string tmp;
        in >> tmp;
        str.assign( tmp.begin(), tmp.end() ); 
        return in;
    }