iterator problem

I am copying an example from a book and it wont compile.

I am trying to copy a vector<string> to a set<String> using transform, as it will make all the strings lower case.

I get the following warning

In instantiation of '_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >; _OIter = std::_Rb_tree_const_iterator<std::__cxx11::basic_string<char> >; _UnaryOperation = void (*)(std::__cxx11::basic_string<char>)]':|

and the following error

no match for 'operator=' (operand types are 'const std::__cxx11::basic_string<char>' and 'void')|

my code is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void workingTogether(){
    using std::vector;
    using std::set;
    using std::cout;
    using std::cin;
    using std::string;

    vector<string> allWords;
    string word;
    cout << "Enter words (quit to quit)";
    while(cin >> word && word != "quit"){
        allWords.push_back(word);
    }

    cout << "You entered the following words: ";
    for_each(allWords.begin(), allWords.end(), display);

    set<string> uniqueWords;
    std::insert_iterator<set<string>> it(uniqueWords, uniqueWords.begin());
    transform(allWords.begin(), allWords.end(), it, ToLower); //error here with 'it'
}
Last edited on
You don't provide a compilable example. However consider this which compiles OK:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <vector>
#include <set>
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

void display(const std::string& str) {
	std::cout << str << "  ";
}

std::string ToLower(std::string str) {
	for (auto& ch : str)
		ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));

	return str;
}

int main() {
	std::vector<std::string> allWords;

	std::cout << "Enter words (quit to quit): ";

	for (std::string word; std::cin >> word && word != "quit"; allWords.push_back(word));

	std::cout << "You entered the following words: ";
	std::for_each(allWords.begin(), allWords.end(), display);
	std::cout << '\n';

	std::set<std::string> uniqueWords;
	std::insert_iterator<std::set<std::string>> it(uniqueWords, uniqueWords.begin());

	std::transform(allWords.begin(), allWords.end(), it, ToLower);
	std::for_each(uniqueWords.begin(), uniqueWords.end(), display);
	std::cout << '\n';
}



Enter words (quit to quit): to TO qe Qe qwe qWE tO quit
You entered the following words: to  TO  qe  Qe  qwe  qWE  tO
qe  qwe  to

Full Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>
#include <set>
#include <iterator>
#include <algorithm>
#include <string>
#include <cctype>


void ToLower(std::string data){
    transform(data.begin(), data.end(), data.begin(), tolower);
}

void display(const std::string & s){
    std::cout << s << ' ';
}

int main(){
    using std::vector;
    using std::set;
    using std::cout;
    using std::cin;
    using std::string;

    vector<string> allWords;
    string word;
    cout << "Enter words (quit to quit)";
    while(cin >> word && word != "quit"){
        allWords.push_back(word);
    }

    cout << "You entered the following words: ";
    for_each(allWords.begin(), allWords.end(), display);
    set<string> uniqueWords;
    std::insert_iterator<set<string>> it(uniqueWords, uniqueWords.begin());
    transform(allWords.begin(), allWords.end(), it, ToLower);

return 0;
}

Last edited on
1
2
3
4
std::string ToLower(std::string data){
    transform(data.begin(), data.end(), data.begin(), tolower);
    return data;
}



Make sure that you actually print the contents of set uniqueWords at the end.
Just been looking at it and I noticed the return type. Thanx for looking anyway. But I got there eventually (18 minutes after you did)

Kind regards.
Last edited on
Topic archived. No new replies allowed.