using a nested vector in C++ - c++

I am trying to implement a vector<int> within a vector<Type> in C++. However whenever I run the following code, I get an error reading
std::vector<std::vector<int> >::const_iterator’ has no member named ‘begin’
std::vector<std::vector<int> >::const_iterator’ has no member named ‘end’
Here is the code:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
typedef vector<int> vector1D ;
typedef vector<vector1D > vector2D ;
void showarr(const vector2D& v)
{
for (vector<vector1D >::const_iterator it1 = v.begin(); it1 != v.end(); ++it1) {
for(vector<int>::const_iterator it2 = *it1.begin(); it2 != *it1.end(); ++it2) {
cout<<*it2<<endl;
}
}
}
int main(int argc, char *argv[])
{
int rownum;
cin>>rownum;
vector2D a;
for ( int i = 0 ; i < rownum ; i++) {
a.push_back(vector1D(rownum,0));
}
showarr(a);
return 0;
}
Any type of help is appreciated.

Try changing:
*it1.begin()
to
it1->begin()

It's being parsed as *(it1.begin()), not (*it1).begin(). Change it to it1->begin().

The problem is in the line containing *itr.begin(). Change it to itr->begin(). This way, you won't get any errors.

Related

Declaring multidimensional vector with variable number of dimensions in C++

I am trying to declare a multidimensional vector with variable number of dimensions (user input).
here is what I have:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
vector< double > data;
int main() {
int numberDimensions = 4;
for (int it = 0; it < numberDimensions; it++){
// Nor sure what to put here
}
return 0;
}
Another solution is by using an if statement at the begining but I was wondering if another solution exists ?
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int numberDimensions = 4;
if (numberDimensions==0)
cout << 'error' << endl;
else if (numberDimensions==1)
vector< double> data;
else if (numberDimensions==2)
vector< vector< double> > data;
else if (numberDimensions==3)
vector< vector< vector< double> > > data;
else if (numberDimensions==4)
vector< vector< vector< vector< double> > > > data;
return 0;
}
Thanks for any suggestions,
As suggested in the comments here is the solution I followed:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
vector< double > data;
int main() {
std::vector<int> parameter1 {34,23,58};
std::vector<int> parameter2 {1,2,3};
data = vector< double > (parameter1.size()*parameter2.size());
calculateResult(data);
// If I want to access the result for Parameter1 = 58 and Parameter = 2 I do:
int index1 = 2
int index2 = 1
double selectedResult = data[index1*parameter1.size()+index2];
return 0;
}

unresolved identifier on .begin() and .end()?

i keep getting this error "unresolved identified" on l_vector.begin() and l_vector.end, specifically on the begin and end functions, why is it not recognizing these simple vector functions?
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <vector>
#include <string>
using namespace std;
// function prototype
int FindIndexOfLargest(vector<int> l_vector);
int main()
{
int FindIndexOfLargest();
return 0;
}
//function definition
int FindIndexOfLargest(vector<int> l_vector)
{
vector<int>:: const_iterator iter;
int current_max_location = 0;
int current_max = 0;
for(iter = l_vector.begin(); iter != l_vector.end(); ++iter)
{
if(*iter > current_max)
{
current_max_location = iter;
}
return current_max;
}
}
I see a few causes for compilation failure.
int FindIndexOfLargest();
You have to pass an std::vector <int> to this function when you call it in main().
And finally, your function int FindIndexOfLargest(vector<int> l_vector) must return an int value.
An alternate way to write this function is to use std::max_element and std::distance.
std::size_t FindIndexOfLargest(const std::vector <int> &input)
{
std::vector<int>::const_iterator max_value =
std::max_element(input.cbegin(), input.cend());
if (max_value != input.cend()) {
return std::distance (input.cbegin(), max_value);
}
// Otherwise, throw an expection or return an error value here.
}

Element with highest occurence in array of strings

I need to find the element with the highest occurrence in an array of strings. I am not sure what to do, since I don't have much experience with this. I don't know pointers / hashtables.
I've already done this for integers, but I can't make it work for strings.
My version:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int a[]={1,2,3,4,4,4,5};
int n = sizeof(a)/sizeof(int );
int *b=new int [n];
fill_n(b,n,0); // Put n times 0 in b
int val=0; // Value that is the most frequent
for (int i=0;i<n;i++)
if( ++b[a[i]] >= b[val])
val = a[i];
cout<<val<<endl;
delete[] b;
return 0;
}
Any help for finding the most frequently occurring element in the array of strings is appreciated!
First, consider using C++ containers like vectors instead of plain arrays. (Search for "array to vector" or such to convert between them if you need.)
Then, if you can use C++11, you can do something like this (without C++11 it would become a bit more lengthy, but still doable):
std::string most_occurred(const std::vector<std::string> &vec) {
std::map<std::string,unsigned long> str_map;
for (const auto &str : vec)
++str_map[str];
typedef decltype(std::pair<std::string,unsigned long>()) pair_type;
auto comp = [](const pair_type &pair1, const pair_type &pair2) -> bool {
return pair1.second < pair2.second; };
return std::max_element(str_map.cbegin(), str_map.cend(), comp)->first;
}
Here is a version compatible with older C++
bool comp(const std::pair<std::string,unsigned long> &pair1,
const std::pair<std::string,unsigned long> &pair2) {
return pair1.second < pair2.second;
}
std::string most_occurred(const std::vector<std::string> &vec) {
std::map<std::string,unsigned long> str_map;
for (std::vector<std::string>::const_iterator it = vec.begin();
it != vec.end(); ++it)
++str_map[*it];
return std::max_element(str_map.begin(), str_map.end(), comp)->first;
}
You could consider using a vector of strings and to use a map to count occurrences:
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main(int argc, char *argv[])
{
vector<string> a;
map<string, int> m;
// fill a with strings
a.push_back("a");
a.push_back("b");
a.push_back("b");
a.push_back("c");
a.push_back("c");
a.push_back("c");
a.push_back("d");
a.push_back("e");
// count occurrences of every string
for (int i = 0; i < a.size(); i++)
{
map<string, int>::iterator it = m.find(a[i]);
if (it == m.end())
m.insert(pair<string, int>(a[i], 1));
else
m[a[i]] += 1;
}
// find the max
map<string, int>::iterator it = m.begin();
for (map<string, int>::iterator it2 = m.begin(); it2 != m.end(); ++it2)
{
if (it2 -> second > it -> second)
it = it2;
}
cout << it -> first << endl;
return 0;
}
This solution is probably not the best one in terms of elegance and efficiency, however it should give the idea.

how to get the first non NULL value in a MAP?

i have an STL map ;
i would like to get the first non NULL value in the map;
is there an efficient/quick way to do that?
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
bool IsNotNull(const pair<const int, int>& i)
{
return i.second != 0;
}
int main() {
map<int, int> m;
m[0] = 0;
m[1] = 1;
map<int, int>::const_iterator it = find_if(m.begin(), m.end(), IsNotNull);
cout << it->second << endl;
return 0;
}
Ideone demo
There's nothing quicker than just looping through and finding what you're looking for
for (map<X,Y>::const_iterator i = m.begin(); i != m.end(); ++i)
{
if (i->second != NULL)
{
// do something with first non-NULL value
break;
}
}

Storing values from a vector to a string as comma seperated values

how can I store the values returned from a function to a string as comma seperated values. Can anyone help me..?
const myVector &vecList = obj.get_List();
vector<myNumVector *>::const_iterator iter;
for (iter= vecList.begin(); iter!= vecList.end(); iter++)
{
string myNum = (*iter)->get_myNum();
string myNumList = ?
//myNumList should be = drt123,ret34,dfghgd234.... if these are the return values
} //can we achive this by use of some sting functions..?
As can be seen from the links I posted, there are lots of ways to do this. Here is, I believe, the simplest:
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <boost/assign/list_of.hpp>
using boost::assign::list_of;
namespace ba = boost::assign;
vector<string> victor = list_of
("Clarence Oveur")
("Roger Murdock")
("Victor Basta");
int main() {
string result;
for(vector<string>::iterator it = victor.begin();
it != victor.end();
++it) {
if(it != victor.begin()) {
result += ", ";
}
result += *it;
}
cout << result << "\n";
}
EDIT: To translate directly to OP's question:
const myVector &vecList = obj.get_List();
vector<myNumVector *>::const_iterator iter;
string myNumlist;
for (iter= vecList.begin(); iter!= vecList.end(); iter++)
{
string myNum = (*iter)->get_myNum();
if(iter!=vecList.begin()) {
nyNumList += ",";
}
myNumList += myNum;
}
EDIT: Simplified by removing bool first from previous solution.
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
int main () {
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
std::stringstream list;
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(list, ","));
std::cout << list.str();
return 0;
}
Outputs: 1,2,3,4,
more modern approach, also solving the trailing ","
#include <string>
#include <numeric>
#include <iostream>
int main() {
const auto v = {1, 2, 3, 4};
const auto list = std::accumulate(begin(v), end(v), std::string{}, [](const std::string& so_far, const auto& next) {
return so_far + (so_far.empty() ? "" : ", ") + std::to_string(next);
});
std::cout << list;
return 0;
}
Yes, this can be achieved using string functions, along with a handful other methods.
Given a string myNumList defined outside the loop, you could simply
myNumList += "," + myNum;
although that would add an extraneous comma in the beinning, so check if iter is pointing there first:
if(iter != vecList.begin())
myNumList += ',';
myNumList += myNum;