Traverse a List Using an Iterator? - c++

I need sample for traversing a list using C++.

The sample for your problem is as follows
#include <iostream>
#include <list>
using namespace std;
typedef list<int> IntegerList;
int main()
{
IntegerList intList;
for (int i = 1; i <= 10; ++i)
intList.push_back(i * 2);
for (IntegerList::const_iterator ci = intList.begin(); ci != intList.end(); ++ci)
cout << *ci << " ";
return 0;
}

To reflect new additions in C++ and extend somewhat outdated solution by #karthik, starting from C++11 it can be done shorter with auto specifier:
#include <iostream>
#include <list>
using namespace std;
typedef list<int> IntegerList;
int main()
{
IntegerList intList;
for (int i=1; i<=10; ++i)
intList.push_back(i * 2);
for (auto ci = intList.begin(); ci != intList.end(); ++ci)
cout << *ci << " ";
}
or even easier using range-based for loops:
#include <iostream>
#include <list>
using namespace std;
typedef list<int> IntegerList;
int main()
{
IntegerList intList;
for (int i=1; i<=10; ++i)
intList.push_back(i * 2);
for (int i : intList)
cout << i << " ";
}

If you mean an STL std::list, then here is a simple example from http://www.cplusplus.com/reference/stl/list/begin/.
// list::begin
#include <iostream>
#include <list>
int main ()
{
int myints[] = {75,23,65,42,13};
std::list<int> mylist (myints,myints+5);
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it != mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}

now you can just use this:
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> intList;
for (int i = 1; i <= 10; ++i)
intList.push_back(i * 2);
for (auto i:intList)
cout << i << " ";
return 0;
}

Related

c++ how to print how many times each integer appears in the STL list

using namespace std;
int main()
{
list<int> numbers; list<int> numb;
for (int i = 0; i<10; i++)
numbers.push_back(rand() % 20);
list<int>::iterator it;
for (it = numbers.begin(); it != numbers.end(); ++it)
{
cout << *it << " ";
}
return 0;
}
I wanted to use std::count() but I am not able to do it right. I tried to do the following:
using namespace std;
int main()
{
list<int> numbers; list<int> numb;
for (int i = 0; i<10; i++)
numbers.push_back(rand() % 20);
list<int>::iterator it;
for (it = numbers.begin(); it != numbers.end(); ++it)
{
cout << *it << " ";
while (it != numbers.begin() && it != numbers.end())
{
++it;
*it = count(it, numbers.begin(), numbers.end());
cout << " " << *it;
}
cout << endl;
}
return 0;
}
But it gives me an error:
binary == no operator found which takes a left hand operator type 'int' (or there is not acceptable conversion).
I know I am doing something wrong.
I also tried a few more things, like int numb = std::count(numbers.begin()), numbers.end(), *it), but it didn't work either. So, I want to know if there is a special operator to count values in a list.
You need to look at the signature for std::count again. It takes three parameters std::count(InputIterator first, InputIterator last, const T& val); and it returns the number of occurrences of val in your data set. So something like this should work for you where theNumber is the number you're counting.
#include <algorithm>
int occurrences = std::count(numbers.begin(), numbers.end(), theNumber);
You are not using iterators correctly (you are modifying it while you are still using it to iterate the list), and you are not calling std::count() correctly.
The code should look more like this instead:
#include <iostream>
#include <list>
#include <algorithm>
#include <cstdlib>
int main()
{
std::list<int> numbers;
int numb;
for (int i = 0; i < 10; i++)
numbers.push_back(std::rand() % 20);
std::list<int>::iterator it;
for (it = numbers.begin(); it != numbers.end(); ++it)
{
numb = std::count(numbers.begin(), numbers.end(), *it);
std::cout << *it << " " << numb << std::endl;
}
/* or:
for (int value : numbers)
{
numb = std::count(numbers.begin(), numbers.end(), value);
std::cout << value << " " << numb << std::endl;
}
*/
return 0;
}
But, like others said, you should use a std::map to track the counts, so you can account for duplicates, eg:
#include <iostream>
#include <list>
#include <map>
#include <cstdlib>
int main()
{
std::list<int> numbers;
std::map<int, int> numb;
for (int i = 0; i < 10; i++)
numbers.push_back(rand() % 20);
for (std::list<int>::iterator it = numbers.begin(); it != numbers.end(); ++it)
numb[*it]++;
/* or:
for (int value : numbers)
numb[value]++;
*/
for (std::map<int, int>::iterator it = numb.begin(); it != numb.end(); ++it)
std::cout << it->first << " " << it->second << std::endl;
/* or:
for (auto &item : numb)
std::cout << item.first << " " << item.second << std::endl;
*/
return 0;
}
Which can be reduced to this:
#include <iostream>
#include <map>
#include <cstdlib>
int main()
{
std::map<int, int> numb;
for (int i = 0; i < 10; i++)
numb[rand() % 20]++;
for (std::map<int, int>::iterator it = numb.begin(); it != numb.end(); ++it)
std::cout << it->first << " " << it->second << std::endl;
/* or:
for (auto &item : numb)
std::cout << item.first << " " << item.second << std::endl;
*/
return 0;
}
In general, using a map is a better approach to your problem, but if you have to solve it using lists here is one possible solution:
#include <iostream>
#include <algorithm>
#include <list>
int main()
{
std::list<int> numbers, unique_num, numb;
int num;
// Create both the original list and a list that
// will be left with only unique numbers
for (int i = 0; i<10; i++){
num = rand() % 20;
numbers.push_back(num);
unique_num.push_back(num);
}
// Sort and select the unique numbers
unique_num.sort();
unique_num.unique();
// Count unique numbers and store the count in numb
std::list<int>::iterator iter = unique_num.begin();
while (iter != unique_num.end())
numb.push_back(count(numbers.begin(), numbers.end(), *iter++));
// Print the results
for(std::list<int>::iterator iter1 = unique_num.begin(), iter2 = numb.begin();
iter2 != numb.end(); iter1++, iter2++)
std::cout<< "Number " << *iter1 << " appears " <<
*iter2 << ( *iter2 > 1 ? " times " : " time" ) << std::endl;
return 0;
}
The program uses another list, unique_num, to hold unique numbers occurring in numbers. That list is initially created identical to numbers and is then sorted and the duplicates are removed.
The program then iterates through numbers in that unique list and uses count to get the number of occurrences of each of them in the original numbers list. The number of occurrences is then stored in a new list, numb.
When printing, the program uses a ternary operator to check whether it should print "time" or "times" depending whether the result implies one or more than one occurrence.
Note - if you want different list values each time you run your program you need to change the random seed using srand. Include the header #include <time.h> in your program and the line srand(time(NULL)); at the beginning of your main.
I suggest you use a map:
map<int, int> counts;
for(int val : Numbers)
++counts[val];
WITH ADDITIONAL MEMORY:
You can use buckets, to get complexity O(N + MAX_NUM). So when MAX_NUM <= N we have O(N):
#include <iostream>
#include <list>
#include <algorithm>
#include <ctime>
const int MAX_NUM = 20;
const int N = 10;
int main() {
std::list<int> numbers;
int buckets[MAX_NUM];
std::fill(buckets, buckets + MAX_NUM, 0);
srand(time(NULL));
for (int i = 0; i < N; i++) numbers.push_back(rand() % MAX_NUM);
// computing
for (auto it = numbers.begin(); it != numbers.end(); ++it) {
buckets[*it]++;
}
//printing answers
for (int i = 0; i < MAX_NUM; i++) {
if (buckets[i]) std::cout << "value " << i << " appears in the list " << buckets[i] << " times." <<std::endl;
}
return 0;
}
For big data i would recommend using std::unordered_map for buckets and then geting complexity O(N) (thanks to hashing):
#include <iostream>
#include <list>
#include <algorithm>
#include <ctime>
#include <unordered_map>
const int N = 10;
const int MAX_NUM = 20;
int main() {
std::list<int> numbers;
std::unordered_map<int, int> buckets;
srand(time(NULL));
for (int i = 0; i < N; i++) numbers.push_back(rand() % MAX_NUM);
// computing
for (auto it = numbers.begin(); it != numbers.end(); ++it) {
buckets[*it]++;
}
//printing answers
for (auto & k_v : buckets) {
std::cout << "value " << k_v.first << " appears in the list " << k_v.second << " times." <<std::endl;
}
return 0;
}
WITHOUT ADDITIONAL MEMORY:
In more universal way, you can use std::vector instead of std::list and std::sort on it, and then count value changes in a simple for. Complexity is O(N log N):
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
const int N = 10;
const int MAX_NUM = 20;
int main() {
std::vector<int> numbers;
srand(time(NULL));
for (int i = 0; i < N; i++) numbers.push_back(rand() % MAX_NUM);
// sorting
std::sort(numbers.begin(), numbers.end());
//printing answers for sorted vector
if (numbers.size() > 0) {
int act_count = 1;
for (int i = 1; i < numbers.size(); i++) {
if (numbers[i] != numbers[i -1]) {
std::cout << "value " << numbers[i-1] << " appears in the list " << act_count << " times." <<std::endl;
act_count = 1;
} else {
act_count++;
}
}
std::cout << "value " << numbers[numbers.size() - 1] << " appears in the list " << act_count << " times." <<std::endl;
}
return 0;
}
You can also do the above on std::list, getting also O(nlogn), but can't use std::sort:
#include <iostream>
#include <list>
#include <ctime>
const int N = 10;
const int MAX_NUM = 20;
int main() {
std::list<int> numbers;
srand(time(NULL));
for (int i = 0; i < N; i++) numbers.push_back(rand() % MAX_NUM);
// sorting
numbers.sort();
//printing answers for sorted list
if (!numbers.empty()) {
int act_count = 0;
auto prev = numbers.begin();
for (auto it = numbers.begin(); it != numbers.end(); it++) {
if (*it != *prev) {
std::cout << "value " << *it << " appears in the list " << act_count << " times." <<std::endl;
act_count = 1;
} else {
act_count++;
}
prev = it;
}
std::cout << "value " << *prev << " appears in the list " << act_count << " times." <<std::endl;
}
return 0;
}

Ampersand in c++ range based for loops

Here is the code:
#include <cmath>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdio>
int main(int argc, char *argv[]) {
const unsigned int max_chars = 100;
char buffer[max_chars];
std::cin.getline(buffer, max_chars, '\n');
unsigned int count = 0;
for (auto c : buffer) {
if (c == '\0') {
break;
}
count++;
}
std::cout << "Input: ===========" << std::endl;
std::cout << buffer << std::endl;
std::cout << "Number of chars ==" << std::endl;
std::cout << std::dec << count << std::endl;
std::cout << "==================" << std::endl;
}
This is adapted from some example code in a c++ text book deliberately dealing with c-style strings, so bear with me.
So I tried two versions of this, one with for (auto c : buffer) and the other with for (auto &c : buffer). Both seemed to work. The question is, what is the difference then?
When you use a link, you work directly with the elements of the container. Otherwise - with a copy. Try this example:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 10;
vector<int> a(n);
vector<int> b(n);
for (auto &key : a) key = rand()%10;
for (auto key : b) key = rand()%10;
for (int i = 0; i < n; i++) cout << a[i];
cout << endl;
for (int i = 0; i < n; i++) cout << b[i];
}
The first one (no &) is a value, the second one (with &) is a reference. A reference, as its name implies, "references" a value, similar to the way a pointer "points" to a value.
Try adding c = 'x'; after your if statement and trying both ways to see the difference here.

error: no match for ‘operator[]’?

I am trying to access the std::map data using key, but I am getting the error
error: no match for ‘operator[]’ (operand types are ‘const std::pair’ and ‘int’)
#include <map>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<map<int, double>> mainData;
for(int i = 0; i < 10; i++) {
map<int, double> data;
data[1] = i;
data[2] = i*2.0;
data[5] = i*7.2;
mainData.push_back(data);
}
for(auto& it1: mainData) {
for(auto& it2: it1) {
cout << it2.first << " " << it2.second << "\n";
cout << it2[5] << "\n"; // Error occurs here
}
cout << "\n";
}
return 0;
}
I think you have a typo here. I think you meant to access it1 instead of it2, because it1 is actually the map and it2 is the pair within the map.
cout << it1[5] << "\n";
Maybe you should use different variable names:
#include <map>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<map<int, double>> mainData;
for(int i = 0; i < 10; i++) {
map<int, double> data;
data[1] = i;
data[2] = i*2.0;
data[5] = i*7.2;
mainData.push_back(data);
}
for(auto& map: mainData) {
for(auto& pair: map) {
cout << pair.first << " " << pair.second << "\n";
cout << map[5] << "\n";
}
cout << "\n";
}
return 0;
}
Maybe you meant to use it1[5]?
it2 is a std::pair (http://www.cplusplus.com/reference/utility/pair/). You meant it1[5] maybe ? Actually doing it2[x] can't work then there is a problem in the code you made
because there are no [] operator for std pair, probably you mean it1[5] instead of it2[5]

c++ How can I count the unique elements in a vector?

I have this c++ code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cmath>
using namespace std;
struct szemelyadat {
string vnev;
string knev;
int szulev;
int szulhonap;
int szulnap;
};
int vfmeret(const char* fajlnev)
{
string temp;
int merettemp = 0;
ifstream meretBE(fajlnev);
while (!meretBE.eof()) {
meretBE >> temp;
merettemp++;
}
meretBE.close();
return merettemp;
}
void vfbeolv(szemelyadat* vek, int elemszam, const char* fajlnev)
{
int i, j;
string stemp, stempresz;
ifstream adatBE(fajlnev);
for (i = 0; i < elemszam; i++) {
adatBE >> stemp;
istringstream sorfolyam(stemp);
j = 0;
while (getline(sorfolyam, stempresz, ';')) {
if (j == 0)
vek[i].vnev = stempresz;
if (j == 1)
vek[i].knev = stempresz;
if (j == 2)
vek[i].szulev = atoi(stempresz.c_str());
if (j == 3)
vek[i].szulhonap = atoi(stempresz.c_str());
if (j == 4)
vek[i].szulnap = atoi(stempresz.c_str());
j++;
}
}
adatBE.close();
}
void vfkiir(szemelyadat* vek, int elemszam)
{
cout << "Vezeteknev "
<< "Keresztnev "
<< "Szuletesiev "
<< "Szuletesihonap "
<< "Szuletesinap " << endl;
for (int i = 0; i < elemszam; i++)
cout << vek[i].vnev << " " << vek[i].knev << " " << vek[i].szulev << ". " << vek[i].szulhonap << ". " << vek[i].szulnap << endl;
}
int main()
{
int n = vfmeret("szuletesi_nevlista.txt");
szemelyadat* vektor;
vektor = new szemelyadat[n];
vfbeolv(vektor, n, "szuletesi_nevlista.txt");
vfkiir(vektor, n);
delete[] vektor;
cin.get();
cin.get();
return 0;
}
I need to cout the count of the unique elements which is in the "vek[i].szulev"
please help me somebody, how can I do this ?
Here's one simple way to count the unique values in a vector:
using Size = ptrdiff_t;
template< class Item >
auto n_unique_items( vector<Item> const& v )
-> Size
{ return set<Item>{ v.begin(), v.end() }.size(); }
Since you insisted on using "your code", you can still utilize std::set to do the counting.
It all boils down to this:
#include <set>
#include <iostream>
void count_items(szemelyadat* vek, int elemszam)
{
std::set<int> counter;
for (int i = 0; i < elemszam; i++)
counter.insert(vek[i].szulev);
std::cout << "The number of unique items is " << counter.size();
}
If you call the count_items once you fill your "vektor", then this will give you then unique number of szulev in the vektor.

How to count duplicates in a vector (C++)

I was working through an exercise in C++ Primer. Actually, I refined my first version. Problem is I not only want to detect duplications in a vector, but also how many times they were duplicated. I'm having trouble with the latter.
Here is my code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> nums{1,3,1,5,7,8,9,7};
sort(nums.begin(), nums.end());
for(unsigned int i = 0; i != nums.size(); ++i){
if(nums[i] == nums[i + 1]){
cout << nums[i] << " is a duplicated number" << endl;
}
}
return 0;
}
EDIT: Also just noticed my logic is flawed. If a number appears more than twice it will print out multiple times it's a duplicate. Which is redundant.
Use a std::map
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
map<int, int> duplicate;
vector<int> nums{1,3,1,5,7,8,9,7,1};
vector<int> nums_sorted{nums};
sort(begin(nums_sorted), end(nums_sorted));
auto beg = begin(nums_sorted) + 1;
for (;beg != end(nums_sorted); ++beg) {
if (*beg == *(beg - 1)) {
duplicate[*beg]++;
}
}
for (const auto& i : duplicate)
cout << i.first << " appear " << i.second+1 << " times" << '\n';
}
Outputs:
1 appear 3 times
7 appear 2 times
You can pair up std::unique<>() with std::distance<>():
std::sort(nums.begin(), nums.end());
auto unique_end = std::unique(nums.begin(), nums.end());
std::cout << std::distance(nums.begin(), unique_end);
You were almost there, here is my suggested solution:
live
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> nums{1,3,1,5,7,8,9,7};
sort(nums.begin(), nums.end());
for(auto it = std::cbegin(nums); it != std::cend(nums); ) {
int dups = std::count(it, std::cend(nums), *it);
if ( dups > 1 )
cout << *it << " is a duplicated number, times: " << dups << endl;
for(auto last = *it;*++it == last;);
}
return 0;
}
A stupid but quick solution:
#include <map>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> nums{1,3,1,5,7,8,9,7,1};
std::map<int, int> dups;
for(int i : nums)
++dups[i];
for(auto& dup : dups)
cout << "Number " << dup.first << " has " << dup.second - 1 << " duplicates\n";
}