Feeds:
Posts
Comments

Posts Tagged ‘c++0x’

what a weird topic to talk about. i don’t even know if this piece
of code is any good. it will probably serve to remind me of the
usage of certain functions etc at a later date. may be it will
scare somebody off and may be some kind stranger will correct my
mistakes and point me to enlightened realms. all this does is to
compute the column sums of a matrix. notice that we maintain our
distance from the for and while loops. i have been admonished
myriads of time for not using the standard library functions so i
have avoided them here. what can i say i simply love the lambda
functions
over functors. although the compiler ultimately uses
functors behind the scenes but i love how clean and separated the
code looks.

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>

int main(){
  // matrix
  std::vector<std::vector<int> > a = {{0,0,0}, {0,0,0}, {0,0,0}};
// c++0x allows initialization lists

  // randomly initialize using lambda expressions
  std::for_each(a.begin(), a.end(), [](std::vector<int>& i){
      std::generate(i.begin(), i.end(), [](){return rand()%10;});
    });

  // display using lamda expressions
  std::for_each(a.begin(), a.end(), [](std::vector<int>& i){
      std::copy(i.begin(), i.end(), std::ostream_iterator<int>(std::cout, "\t"));
      std::cout << std::endl;
    });

  // zero vector
  std::vector<int> zeros = {0,0,0};
  
  // column sums
  std::vector<int> csums = std::accumulate(a.begin(), a.end(), zeros, 
[](std::vector<int>& i, std::vector<int>& j)->std::vector<int>{
std::transform(i.begin(), i.end(), j.begin(), i.begin(), std::plus<int>());
return i;
});

  // display the column sums using ostream_iterators
  std::copy(csums.begin(), csums.end(), std::ostream_iterator<int>(std::cout, "\t"));

  return 0;
}

i now explain bits and parts of the mess

  // randomly initialize
  std::for_each(a.begin(), a.end(), [](std::vector<int>& i){
      std::generate(i.begin(), i.end(), [](){return rand()%10;});
    });

the above initializes the matrix a. since each element of a is itself a vector i use a lambda function that initializes the individual vectors. the lambda is as follows:

[](std::vector<int>& i){std::generate(i.begin(), i.end(), [](){return rand()%10;});}

the actual adding of columns is done using the std::accumulate() library function

  // column sums
  std::vector<int> csums = std::accumulate(a.begin(), a.end(), zeros, 
[](std::vector<int>& i, std::vector<int>& j)->std::vector<int>{
std::transform(i.begin(), i.end(), j.begin(), i.begin(), std::plus<int>());
return i;
});

that uses the lamda

[](std::vector<int>& i, std::vector<int>& j)->std::vector<int>{
std::transform(i.begin(), i.end(), j.begin(), i.begin(), std::plus<int>());
return i;
}

Read Full Post »

whenever possible one should use the standard library tools provided. std::mismatch() returns a pair of iterators to the first mismatch. examples of this are abundant on the WWW but i wanted to find all the mismatches and could not find an example online, hence this post. i don’t think the code is elegant so please let me know better versions of my code.

#include <iostream>
#include <vector>
#include <algorithm>
 
int main()
{
  std::vector<unsigned> a = {1,2,3,4,5,6};
  std::vector<unsigned> b = {1,2,33,4,55,66};
 
  // find the first mismatch
  auto p = std::mismatch(a.begin(), a.end(), b.begin());

  while(p.first != a.end())
    {
      std::cout << *p.first << std::endl;
      std::cout << *p.second << std::endl;
      p = std::mismatch(++p.first, a.end(), ++p.second);
    }
 
return 0;
}

havent’ tried the above on list container but i believe list iterators support the increment operator.

don’t forget to compile the above using -std=c++0x

Read Full Post »

i have always defined matrices of two dimensions as a vector of a vector container as follows

vector< vector <double> > v; // note the distance between the two right angle brakets just before v

however i recently learned that c++0x allows one to get rid of the extra space between the right angle brackets as in the following

vector< vector <double> > v_old_style; 
vector< vector <double>> v_new_style; 

however emacs 23 breaks indentation when faced with this format

i also use 3 and 4 dimensional tensor like objects for which i would much like the following:

vector < vector < vector < vector <double> > > > v1; // don't like it
vector<vector<vector<vector<double>>>> v2; // like this a lot 

for me at least the saving in horizontal space matters when i am comparing code side by side … also its quite easy to figure out the dimensionality of the tensor at one glance.

i don’t know how to fix this 😦 help me please

Read Full Post »