iterator a set in array c++ - c++

#include <iostream>
#include <unordered_set>
using namespace std;
void arraySet(unordered_set<int> n[]){
for(auto it1 = n->begin(); it1!= n->end(); it1++){
cout << "n[" <<*it1<<"]: ";
// for(auto it = it1->begin(); it!= it1->end(); it++){
// cout << *it <<" ";
// }
cout << endl;
}
}
int main()
{
unordered_set<int> n[3];
n[0].insert(734);
n[0].insert(23);
n[0].insert(634);
n[1].insert(2);
n[1].insert(1);
n[2].insert(1);
arraySet(n);
return 0;
}
Can anyone help me explain how to iterator through this set inside array. I believe an easy way is to convert it to set inside vector.

Size of the array needs to be passed as well to the function for you to be able to iterate through all the sets of the array. With just passing the pointer, size cannot be determined and dereferencing n->begin will iterate through only the first set.
void arraySet(unordered_set<int> n[], int size) {
for (auto i = 0; i < size; i++) {
for (auto it1 = n[i].begin(); it1 != n[i].end(); it1++) {
cout << "n[" << *it1 << "]: ";
// for(auto it = it1->begin(); it!= it1->end(); it++){
// cout << *it <<" ";
// }
cout << endl;
}
}
}
int main()
{
unordered_set<int> n[3];
n[0].insert(734);
n[0].insert(23);
n[0].insert(634);
n[1].insert(2);
n[1].insert(1);
n[2].insert(1);
arraySet(n,3);
return 0;
}
Or you could use std::vector to contain the sets and pass it instead.
std::vector<unordered_set<int>> n(3); // Sets the size to 3 elements
The function definition would be changed to
void arraySet(std::vector<unordered_set<int>>& n) {
for (size_t i = 0; i < n.size(); i++) {
for (auto it1 = n[i].begin(); it1 != n[i].end(); it1++) {
cout << "n[" << *it1 << "]: ";
// for(auto it = it1->begin(); it!= it1->end(); it++){
// cout << *it <<" ";
// }
cout << endl;
}
}
}

Related

C++ => No constructor could take the source type, or constructor overload resolution was ambiguous

I have a simple student project and I need some help. I have got a message with some kind of an error that i can't fix. Please help me with a solution. The project is in C++. Below is my code:
#include <iostream>
#include <stdlib.h>
#include <list>
#include <set>
using namespace std;
struct V
{
char name;
list <V> to_v;
};
list <V> graph_v;
set <char> PoseteniVarhove;
list <V> ::iterator ukaz(char name)
{
list <V> ::iterator retukaz = (list <V> ::iterator) NULL; // <-- ERROR HERE
for (list <V> ::iterator it = graph_v.begin(); it != graph_v.end(); it++)
if ((*it).name == name)
retukaz = it;
return retukaz;
}
void MakeGraph()
{
int n, br;
V v;
list <V> ::iterator it;
char name;
do
{
cout << "Broy varhove(1..26): "; cin >> n;
} while (n < 1 || n > 26);
graph_v.clear();
for (int c = 0; c < n; c++)
{
v.name = (char)('A' + c);
v.to_v.clear();
graph_v.push_back(v);
}
for (it = graph_v.begin(); it != graph_v.end(); it++)
{
cout << endl;
cout << "Vrah " << (*it).name << endl;
cout << "Kam kolko drugi varha ima nasocheni rebra? ";
cin >> br;
for (int k = 0; k < br; k++)
{
cout << "Rebro kam vrah #" << 1 + k << endl;
cout << "Ime na varha('A'..'Z'): ";
cin >> name;
v.name = name;
v.to_v.clear();
v.to_v.push_back(*ukaz(name));
(*it).to_v.push_back(v);
}
}
}
void PrintGraph()
{
list <V> ::iterator it, it1;
for (it = graph_v.begin(); it != graph_v.end(); it++)
{
cout << endl << "Ot vrah " << (*it).name << " kam varhove ";
for (it1 = (*it).to_v.begin(); it1 != (*it).to_v.end(); it1++)
cout << (*it1).name << " ";
}
cout << endl;
}
bool Path(char beginVrah, char TekVrah, char Target)
{
bool P = false;
list <V> ::iterator uk, it;
if (PoseteniVarhove.count(TekVrah) == 0)
{
uk = ukaz(TekVrah);
PoseteniVarhove.insert(TekVrah);
for (it = (*uk).to_v.begin(); it != (*uk).to_v.end(); it++)
{
if ((*it).name == Target)
return true;
else
if ((*it).name != beginVrah && !P)
P = Path(beginVrah, (*it).name, Target);
}
}
return P;
}
void Paths()
{
int Count = 0;
bool P;
list <V> ::iterator it, it1;
for (it = graph_v.begin(); it != graph_v.end(); it++)
for (it1 = it; it1 != graph_v.end(); it1++)
if (it != it1)
{
P = false;
PoseteniVarhove.clear();
if (Path((*it).name, (*it).name, (*it1).name))
P = true;
PoseteniVarhove.clear();
if (Path((*it1).name, (*it1).name, (*it).name) && P)
{
cout << (*it).name << ' ' << (*it1).name << endl;
Count++;
}
}
cout << "Obshto " << Count << " dvoyki" << endl << endl;
}
int main()
{
char ch[256];
int choice;
cout << "Graph" << endl;
do
{
cout << endl << "1. Vavezhdane na Graph" << endl;
cout << "2. Izvezhdane na Grapha" << endl;
cout << "3. Paths" << endl;
cout << "4. Kray" << endl;
do
{
cout << "(1,2,3,4): ";
cin >> ch;
choice = atoi(ch);
} while (choice < 1 || choice > 4);
switch (choice)
{
case 1:
MakeGraph();
break;
case 2:
PrintGraph();
break;
case 3:
Paths();
break;
case 4:
break;
default:
break;
}
} while (choice < 4);
return 0;
}
The following error appears:
No constructor could take the source type, or constructor overload resolution was ambiguous
MSVC points to NULL in the first line of the function ukaz.
A "NULL" value for an iterator is usually .end(). It's not entirely clear to me what ukaz means, but it looks like you're trying to do something similar to a find.
As such, if nothing is found, I'd return the .end() of your list. Note that this is also what std::find does:
Return value
Iterator to the first element satisfying the condition or last if no such element is found.
If you want the last occurrence of the value, you can loop using reverse iterators and return immediately when you find something. That way you don't even need to temporary iterator at all. That could look like:
list <V> ::iterator ukaz(char name)
{
for (list <V> ::iterator it = graph_v.rbegin(); it != graph_v.rend(); it++)
if ((*it).name == name)
return it;
return graph_v.end(); // <-- default return val if nothing is found
}

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;
}

How to peek next and prev elements in a boost set<cpp_int>

I am trying to store boost integers cpp_int in an ordered set and check for next and prev elements using below code:
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/unordered_set.hpp>
#include <iostream>
namespace mp = boost::multiprecision;
using boost::unordered_set;
using namespace std;
int main() {
set<mp::cpp_int> st;
set<mp::cpp_int>::iterator it, it1, it2;
//pair<set<mp::cpp_int>::iterator,bool> res;
boost::tuples::tuple<set<mp::cpp_int>::iterator, bool> tp;
int i = 0, temp;
while(i<10){
cin>>temp;
tp = st.insert(temp);
it = get<0>(tp);
it1 = prev(it);
it2 = next(it);
cout<<*it1<<endl;
//cout<<*it2<<endl;
i++;
}
return 0;
}
However, the above code does not work as expected and crashes after couple of inputs. One such crashing sequence of inputs is:
0
1
2
3
4
0
What is the proper way of using set and iterators when using boost?
You need to check that there is a previous / next element before dereferencing the it1 and it2, e.g.:
std::set<mp::cpp_int> s;
for (size_t i = 0; i < 10; ++i){
std::cin >> temp;
auto p = s.insert(temp);
if (p.second) { // insertion succeed
auto it = p.first;
std::cout << "Inserted: " << *it << '\n';
if (it != s.begin()) { // not the first, there is a previous element
auto it1 = std::prev(it);
std::cout << "Previous: " << *it1 << '\n';
}
else {
std::cout << "Previous: None\n";
}
auto it2 = std::next(it);
if (it2 != s.end()) { // there is a next element
std::cout << "Next: " << *it2 << '\n';
}
else {
std::cout << "Next: None\n";
}
}
}
Also, if you want to find the previous and next elements of an existing element, you should use std::set::find, not std::set::insert:
auto it = s.find(temp);
if (it != s.end()) {
// Same code as above.
}

Inserting list elements to vector, getting additional unwanted values C++

In my C++ script, I want to insert some elements of a list into a vector (from the beginning of list to a specific position "it"), and then try to add the vector at the top of the list and keeping the same order of the vector but I get unwanted additional elements in the vector.
Here is my code:
#include <iostream>
#include <iterator>
#include <vector>
#include <list>
int main() {
std::list<int> mylist;
for (int i = 0; i < 10; i++)
mylist.push_back(i * 10);
for (std::list<int>::iterator i = mylist.begin(); i <= mylist.end(); ++i) {
std::cout << *i << ", ";
}
std::cout << std::endl;
std::advance(it, 6);
std::cout << "The 6th element in mylist is: " << *it << std::endl;
// The vector that will contain mylist elements
std::vector<int> intAdeplacer;
intAdeplacer.insert(intAdeplacer.end(), mylist.begin(), it);
std::cout << std::endl;
std::cout << "Nombre d'éléments dans le vecteur : " << intAdeplacer.size() << std::endl;
std::cout << std::endl;
// see the content of the vector
std::cout << "Le vecteur de deplacement contient : " << std::endl;
for (std::vector<int>::const_iterator i = intAdeplacer.begin();
i <= intAdeplacer.end(); ++i) {
std::cout << *i << ", ";
}
I get this output:
Le vecteur de deplacement contient :
0, 10, 20, 30, 40, 134985,
134985 is not wanted..
// Insert in front of the list the values of the vector and keeping the same order of elements in the vector
for (std::vector<int>::const_iterator i = intAdeplacer.end();
i >= intAdeplacer.begin(); --i) {
mylist.push_front(*i);
}
std::cout << std::endl;
std::cout << std::endl;
std::cout << "nouvelle composition de mylist : " << std::endl;
for (std::list<int>::const_iterator j = mylist.begin(); j != mylist.end();
++j) {
std::cout << *j << ", ";
}
std::cout << std::endl;
std::cout << std::endl;
// erasing the added elements from mylist
std::list<int>::iterator debut = mylist.begin();
std::list<int>::iterator fin = mylist.end();
std::advance(fin, 6);
mylist.erase(debut, fin);
for (std::list<int>::iterator j = mylist.begin(); j <= mylist.end(); ++j) {
std::cout << *j << ", ";
}
return 0;
}
If it is an iterator pointing at the 6th element of your list, the following insert() will insert from begin() (included) to it (excluded) into the vector:
intAdeplacer.insert(intAdeplacer.end(), mylist.begin(), it);
So you'll have only 5 elements, from 0 to 40. Unfortunately your printing loop does include the end() of the vector, which is out of range. This is why you get this strange trailing number.
Just correct your loop into:
for (auto i = intAdeplacer.begin(); i != intAdeplacer.end(); ++i) {
std::cout << *i << ", ";
}
Or consider this alternate range-for syntax:
for (auto &element: intAdeplacer) {
std::cout << element << ", ";
}

How to print a vector array?

I have a vector array called nVectors.
vector<int>* nVectors[21];
for (int i = 1; i <= 20; i ++) {
nVectors[i] = generateVector(i);
}
I can print all the members of a single vector, but when it comes to the vector array, I still don't know how to print all the vectors in an array.
Maybe an iterator through all the member of a vector array and print using my predefined method pvector can solve this problem? But I don't know how to iterate in gdb.
std::array<std::vector<int>*, 21> nVectors;
for(std::array<std::vector<int>*>::iterator i = nVectors.begin();
i != nVectors.end();
++i)
{
for(std::vector<int>::iterator it = (*i)->begin();
it != (*i)->end();
++it)
{
std::cout << *it << " ";
}
}
std::cout << std::endl;
Or, in C++11:
std::vector<int>* nVectors[21];
for(auto &i : nVectors)
{
for(auto &it : i)
{
std::cout << *it << " ";
}
}
std::cout << std::endl;