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;
}
Related
my code copies the map in the same order
map <string, int> to vector <string, int>
I want this instead
map <string, int> to vector <int, string>
is it possible with std copy?
#include <iostream>
#include <vector>
#include <map>
#include <iterator>
#include <fstream>
using namespace std;
int main(){
fstream fs;
fs.open("test_text.txt");
if(!fs.is_open()){
cout << "could not open file" << endl;
}
map <string, int> mp;
string word;
while(fs >> word){
for(int i = 0; i < word.length(); i++){
if(ispunct(word[i])){
word.erase(i--, 1);
}
}
if(mp.find(word) != mp.end()){
mp[word]++;
}
}
vector <pair <string, int> > v(mp.size());
copy(mp.begin(), mp.end(), v.begin());
return 0;
}
Lots of different ways, but this will work
vector<pair<int, string>> v;
v.reserve(mp.size());
for (const auto& p : mp)
v.emplace_back(p.second, p.first);
Does't seem to be possible with std::copy since your value types are different and the source is not convertible to the destination. It should be possible with std::transform.
I have a 2D vector of strings and want to count how many times a certain word is repeated. For example:
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector< vector<string> > vec(4, vector<string>(4, "word") );
count( vec.begin(), vec.end(), "certain word" );
}
But the above gives errors. How can I do this?
You need to run count on search individual vector and sum the results:
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector< vector<string> > vec(4, vector<string>(4, "string of words") );
size_t sum = 0;
for(auto& v: vec) {
sum += count( v.begin(), v.end(), "certain word" );
}
}
I am continually writing something akin to
std::vector< std::vector< double > > A(N, std::vector< double >(M));
and I would like to replace this with something like
matrix A(N,M);
by using a #define directive. I've looked at #define directives and think I can create a function like matrix(A,N,M) that would declare a vector of vectors as follows:
#define matrix(A, N, M) std::vector< std::vector< double > > A(N, std::vector< double >(M))
but I would rather not declare my matrices as matrix(A,N,M), but rather matrix A(N,M). My question is - how do I use the #define directives to account for changing a variable name?
You can use typedef and define type, something like that:
#include <vector>
using namespace std;
int main()
{
int N = 10;
typedef std::vector< std::vector<double> matrix;
matrix A(N, std::vector< double >(N));
return 0;
}
or more safety (if you don't know, that matrix will be right)
int main()
{
int N = 10;
typedef std::vector< std::array<double, 5> > matrix;
matrix A(N, std::array< double , 5 >());
return 0;
}
my wrapper for matrix with vectors
#include <iostream>
#include <vector>
#include <exception>
#include <algorithm>
template< typename T >
class WrapperMatrix
{
public:
WrapperMatrix(const int& weight, const int& length);
void pushLine(const std::vector<T>&&);
void pushColumn(const std::vector<T>&&);
void display();
private:
std::vector<std::vector<T>> matrix;
};
template<typename T>
WrapperMatrix<T>::WrapperMatrix(const int& weight, const int& length)
{
this->matrix = std::vector<std::vector<T>>(weight, std::vector<T>(length));
}
template <typename T>
void WrapperMatrix<T>::pushLine(const std::vector<T>&& newLine)
{
if (newLine.size() == this->matrix.at(0).size())
matrix.emplace_back(std::move(newLine));
else
throw std::invalid_argument("Invalis syntax");
}
template <typename T>
void WrapperMatrix<T>::pushColumn(const std::vector<T>&& newColumn)
{
if (newColumn.size() == this->matrix.size())
{
for (int i = 0; i < matrix.size(); ++i)
matrix.at(i).emplace_back(std::move(newColumn.at(i)));
}
else
throw std::invalid_argument("Invalid syntax");
}
template<typename T>
void WrapperMatrix<T>::display()
{
for (int i = 0; i < matrix.size(); ++i)
{
for (int j = 0; j < matrix.at(0).size(); ++j)
std::cout << matrix.at(i).at(j);
std::cout << std::endl;
}
}
int main()
{
std::vector<int> v1{ 1,2,3,4,5 };
std::vector<int> v2{ 1,2,3,4,5,6 };
std::vector<int> v3{ 2,3,4,5,6 };
WrapperMatrix<int> vw(5,5);
try {
vw.pushLine(std::move(v1));
vw.pushColumn(std::move(v2));
//vw.pushLine(std::move(v3));
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
vw.display();
return 0;
}
Alternative answer to typedef
using matrix = std::vector< std::vector<double>>;
This form can be more readable, especially with function and array types. E.g. using arr10 = Foo[10] is clearer than typedef Foo arra10[10]. The = sign clearly separates what's being defined and how it's defined.
(Ignoring the whole "matrix is not a vector of vectors" discussion)
The code is :
#include <map>
#include <utility>
typedef struct
{
int d_number;
int o_number;
} d_o_pair;
std::set<d_o_pair> d_o_set;
std::map<int, d_o_set> my_map;
}
i want to insert into the map. but i am not able to
. i was using like this : this->my_map[5].insert(make_pair(0, 2)). the compiler throws me error telling no function matches call to insert
Following sample code tell us inserting into map. For inserting to set, you need to overload the '<' operator to define the ordering condition in the structure with a constructor and insert in a similar fashion using insert function.
#include<iostream>
#include<map>
using namespace std;
typedef struct
{
int d_number;
int o_number;
}d_o_number;
int main()
{
d_o_number s1;
s1.d_number = 100;
s1.o_number = 1000;
std::map<int, d_o_number> d_o_map;
d_o_map.insert(std::pair<int, d_o_number>(0, s1));
// showing contents:
std::map<int,d_o_number>::iterator it = d_o_map.begin();
std::cout << "d_o_map contains:\n";
for (it=d_o_map.begin(); it!=d_o_map.end(); ++it)
std::cout << it->first << " => " << it->second.d_number<<","<<it->second.o_number << '\n';
return 0;
}
Try the following
#include <map>
#include <set>
#include <utility>
typedef std::pair<int, int> d_o_pair;
typedef std::set<d_o_pair> d_o_set;
int main()
{
std::map<int, d_o_set> my_map;
my_map[5].insert( std::make_pair( 0, 2 ) );
}
Or the following
#include <map>
#include <set>
#include <utility>
typedef struct d_o_pair
{
int d_number;
int o_number;
bool operator <( const d_o_pair &rhs ) const
{
return d_number < rhs.d_number || ( !( rhs.d_number < d_number ) && ( o_number < rhs.o_number ) );
}
} d_o_pair;
typedef std::set<d_o_pair> d_o_set;
int main()
{
std::map<int, d_o_set> my_map;
my_map[5].insert( { 0, 2 } );
}
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.