string issue

I am trying to solve the below problem.

https://www.chegg.com/homework-help/questions-and-answers/amazonian-team-responsible-maintaining-monetary-transaction-service-transactions-tracked-l-q99513835
https://www.chegg.com/homework-help/questions-and-answers/beta-t-read-text-switch-theme-1-amazon-transaction-

Below is the code done so far. Any suggestions here?
How can I test it?

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
42
#include <algorithm>
#include <unordered_map>
#include <string>
#include <sstream>

using namespace std;

vector<string> finddatalog(vector<string>& log, int threshld) {
    vector<string> res;
    unordered_map <string, int> umap;
    vector<string> tmp;
    int id;
    string word;
    for (id = 0; id < log.size(); id++) {
        stringstream strss(log[id]);
        while (strss >> word) {
            tmp.push_back(word);
        }
        if (tmp[0] != tmp[1]) {
            umap[tmp[0]]++;
            umap[tmp[1]]++;
        }
        else
            umap[tmp[0]]++;
        tmp.clear();
    }
    for (auto &i1r : umap) {
        if (i1r.second >= threshld)
            result.push_back(i1r.first);
    }

    sort(res.begin(), res.end(), [](const string& i, const string& j)
                                            {return stoi(i) > stoi(j); });

    return res;
}

int main() {

}

Well I've looked at the chegg site indicated. It describes the layout of the log file - but what is expected to be done with this data? Reading a sample data file in that format into a std::vector of an appropriate struct is fairly easy. But then what?? The ids' are unsigned int and the amount is a double.
I didn't see a question, just a description of the logfile format. Not sure what you're supposed to do with it.

I any event, your code doesn't seem to match that description, you don't respect the 3 input fields for start. Also that result local variable doesn't seem to be used.
To simply read a data file of this format and display it's contents, then consider:

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

struct Log {
	unsigned sender {};
	unsigned receipt {};
	double amount {};
};

int main() {
	std::ifstream ifs("log.dat");

	if (!ifs)
		return (std::cout << "Cannot open log file\n"), 1;

	std::vector<Log> data;

	for (Log l; ifs >> l.sender >> l.receipt >> l.amount; data.emplace_back(l.sender, l.receipt, l.amount));

	for (const auto& [s, r, a] : data)
		std::cout << s << "  " << r << "  " << a << '\n';
}


Data file is:


123456789 234567890 123.45
345678901 456789012 234.78
567889012 678901234 456.87


which displays:


123456789  234567890  123.45
345678901  456789012  234.78
567889012  678901234  456.87

Last edited on
Below are updated links.

https://www.chegg.com/homework-help/questions-and-answers/amazonian-team-responsible-maintaining-monetary-transaction-service-transactions-tracked-l-q99513835

https://www.chegg.com/homework-help/questions-and-answers/beta-t-read-text-switch-theme-1-amazon-transaction-logs-example-question-amazonian-team-re-q101271274


Below is updated 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

#include <algorithm>
#include <unordered_map>
#include <string>
#include <sstream>

using namespace std;

vector<string> finddatalog(vector<string>& log, int threshld) {
    vector<string> res;
    unordered_map <string, int> umap;
    vector<string> tmp;
    int id;
    string word;
    for (id = 0; id < log.size(); id++) {
        stringstream strss(log[id]);
        while (strss >> word) {
            tmp.push_back(word);
        }
        if (tmp[0] != tmp[1]) {
            umap[tmp[0]]++;
            umap[tmp[1]]++;
        }
        else
            umap[tmp[0]]++;
        tmp.clear();
    }
    for (auto &i1r : umap) {
        if (i1r.second >= threshld)
            res.push_back(i1r.first);
    }

    sort(res.begin(), res.end(), [](const string& i, const string& j)
                                            {return stoi(i) > stoi(j); });

    return res;
}

int main() {

}


OUTPUT: Below are errors.
main.cpp:8:16: error: implicit instantiation of undefined template 'std::vector<std::string>'
vector<string> finddatalog(vector<string>& log, int threshld) {
               ^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/iosfwd:260:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS vector;
                           ^
main.cpp:9:20: error: implicit instantiation of undefined template 'std::vector<std::string>'
    vector<string> res;
                   ^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/iosfwd:260:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS vector;
                           ^
main.cpp:11:20: error: implicit instantiation of undefined template 'std::vector<std::string>'
    vector<string> tmp;
                   ^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/iosfwd:260:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS vector;
                           ^
main.cpp:14:26: error: implicit instantiation of undefined template 'std::vector<std::string>'
    for (id = 0; id < log.size(); id++) {
                         ^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/iosfwd:260:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS vector;
                           ^
main.cpp:15:31: error: type 'vector<string>' (aka 'vector<basic_string<char, char_traits<char>, allocator<char>>>') does not provide a subscript operator
        stringstream strss(log[id]);
                           ~~~^~~
5 errors generated.

Last edited on
Well the first error is because you haven't got:

 
#include <vector> 

Below is modified code. Is this correct?

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <algorithm>
#include <unordered_map>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>

using namespace std;

vector<string> finddatalog(vector<string>& log, int threshld) {
    vector<string> res;
    unordered_map <string, int> umap;
    vector<string> tmp;
    int id;
    string word;
    for (id = 0; id < log.size(); id++) {
        stringstream strss(log[id]);
        while (strss >> word) {
            tmp.push_back(word);
        }
        if (tmp[0] != tmp[1]) {
            umap[tmp[0]]++;
            umap[tmp[1]]++;
        }
        else
            umap[tmp[0]]++;
        tmp.clear();
    }
    for (auto &i1r : umap) {
        if (i1r.second >= threshld)
            res.push_back(i1r.first);
    }

    sort(res.begin(), res.end(), [](const string& i, const string& j)
                                            {return stoi(i) > stoi(j); });

    return res;
}

int main() {

   vector<string> ids = {"123456789 234567890 45", "293230 38240 24",
                         "123456788 234567891 15", "293230 38240 78",
                         "123456790 234567892 23", "293230 345377 14",
                         "123456791 234567893 23"};
    vector<string> resl(finddatalog(ids, 3));
    for (auto &s2: resl)
        cout<<s2<<" ";

    return 0;

}

OUTPUT: 293230 
Last edited on
Using the given code, then this compiles:

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
42
43
44
45
46
#include <iostream>
#include <vector>
#include <unordered_map>
#include <sstream>
#include <algorithm>

std::vector<std::string> finddatalog(const std::vector<std::string>& log, int threshld) {
	std::vector<std::string> res;
	std::unordered_map <std::string, int> umap;
	std::vector<std::string> tmp;
	int id;
	std::string word;

	for (id = 0; id < log.size(); id++) {
		std::stringstream strss(log[id]);

		while (strss >> word) {
			tmp.push_back(word);
		}

		if (tmp[0] != tmp[1]) {
			umap[tmp[0]]++;
			umap[tmp[1]]++;
		} else
			umap[tmp[0]]++;
		tmp.clear();
	}

	for (auto& i1r : umap) {
		if (i1r.second >= threshld)
			res.push_back(i1r.first);
	}

	std::sort(res.begin(), res.end(), [](const std::string& i, const std::string& j) {return std::stoi(i) > std::stoi(j); });

	return res;
}

int main() {
	const std::vector<std::string> logs { " 88 99 200 " , " 88 99 300 " , " 99 32 100 " , " 12 12 15 " };

	const auto res { finddatalog(logs, 2) };

	for (const auto& r : res)
		std::cout << r << '\n';
}


which displays:


99
88

Modified code again. But how to keep order of users id?

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <algorithm>
#include <unordered_map>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>

using namespace std;

vector<string> finddatalog(vector<string>& log, int threshld) {
    vector<string> res;
    unordered_map <string, int> umap;
    vector<string> tmp;
    int id;
    string word;
    for (id = 0; id < log.size(); id++) {
        stringstream strss(log[id]);
        while (strss >> word) {
            tmp.push_back(word);
        }
        if (tmp[0] != tmp[1]) {
            umap[tmp[0]]++;
            umap[tmp[1]]++;
        }
        else
            umap[tmp[0]]++;
        tmp.clear();
    }
    for (auto &i1r : umap) {
        if (i1r.second >= threshld)
            res.push_back(i1r.first);
    }

    sort(res.begin(), res.end(), [](const string& i, const string& j)
                                            {return stoi(i) > stoi(j); });

    return res;
}

int main() {

   vector<string> ids = {"123456789 234567890 45", "293230 38240 24",
                         "123456788 123456789 15", "293230 38240 78",
                         "123456789 123456788 23", "293230 123456789 14",
                         "123456788 123456788 23"};
    vector<string> resl(finddatalog(ids, 3));
    for (auto &s2: resl)
        cout<<s2<<" ";

    return 0;

}

OUTPUT: 123456789 123456788 293230 
Last edited on
OK. Consider:

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
#include <iostream>
#include <vector>
#include <map>
#include <sstream>
#include <string>

std::vector<std::string> finddatalog(const std::vector<std::string>& log, unsigned threshld) {
	std::vector<std::string> res;
	std::map <std::string, unsigned> idmap;

	for (const auto& l : log) {
		std::istringstream iss(l);
		std::string id1, id2;

		iss >> id1 >> id2;
		++idmap[id1];

		if (id1 != id2)
			++idmap[id2];
	}

	for (const auto& [id, cnt] : idmap)
		if (cnt >= threshld)
			res.push_back(id);

	return res;
}

int main() {
	const std::vector<std::string> ids { "123456789 234567890 45", "293230 38240 24",
						 "123456788 123456789 15", "293230 38240 78",
						 "123456789 123456788 23", "293230 123456789 14",
						 "123456788 123456788 23" };

	for (const auto& r : finddatalog(ids, 3))
		std::cout << r << '\n';
}


which displays:


123456788
123456789
293230


Last edited on
Note that the sort order for treating the ids as strings or as numbers is different. If the ids's are treated as numbers, then 293230 would be first. Consider:

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
#include <iostream>
#include <vector>
#include <map>
#include <sstream>
#include <string>

using ID_t = unsigned;
//using ID_t = std::string;

std::vector<ID_t> finddatalog(const std::vector<std::string>& log, unsigned threshld) {
	std::vector<ID_t> res;
	std::map <ID_t, unsigned> idmap;

	for (const auto& l : log) {
		std::istringstream iss(l);
		ID_t id1, id2;

		iss >> id1 >> id2;
		++idmap[id1];

		if (id1 != id2)
			++idmap[id2];
	}

	for (const auto& [id, cnt] : idmap)
		if (cnt >= threshld)
			res.push_back(id);

	return res;
}

int main() {
	const std::vector<std::string> ids { "123456789 234567890 45", "293230 38240 24",
						 "123456788 123456789 15", "293230 38240 78",
						 "123456789 123456788 23", "293230 123456789 14",
						 "123456788 123456788 23" };

	for (const auto& r : finddatalog(ids, 3))
		std::cout << r << '\n';
}


which displays:


293230
123456788
123456789

Topic archived. No new replies allowed.