Scilab: improve STL vector example (test all argument passing modes)
This commit is contained in:
parent
87e0005276
commit
d1beaef1ba
4 changed files with 36 additions and 20 deletions
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
#include "example.hxx"
|
||||
|
||||
#include <iterator>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
// double vectors
|
||||
|
||||
std::vector<double> create_dvector(const int size, const double value)
|
||||
|
|
@ -9,19 +13,19 @@ std::vector<double> create_dvector(const int size, const double value)
|
|||
return std::vector<double>(size, value);
|
||||
}
|
||||
|
||||
double sum_dvector(const std::vector<double> dvector)
|
||||
double sum_dvector(const std::vector<double> in_dvector)
|
||||
{
|
||||
double sum = 0;
|
||||
for (int i = 0; i < dvector.size(); i++)
|
||||
for (int i = 0; i < in_dvector.size(); i++)
|
||||
{
|
||||
sum += dvector[i];
|
||||
sum += in_dvector[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
void concat_dvector(std::vector<double>& dvector, const std::vector<double> other_dvector)
|
||||
void concat_dvector(std::vector<double>& inout_dvector, const std::vector<double>& in_dvector)
|
||||
{
|
||||
dvector.insert(dvector.end(), other_dvector.begin(), other_dvector.end());
|
||||
inout_dvector.insert(inout_dvector.end(), in_dvector.begin(), in_dvector.end());
|
||||
}
|
||||
|
||||
// int vectors
|
||||
|
|
@ -31,19 +35,19 @@ std::vector<int> create_ivector(const int size, const int value)
|
|||
return std::vector<int>(size, value);
|
||||
}
|
||||
|
||||
int sum_ivector(const std::vector<int> ivector)
|
||||
int sum_ivector(const std::vector<int> in_ivector)
|
||||
{
|
||||
int sum = 0;
|
||||
for (int i = 0; i < ivector.size(); i++)
|
||||
for (int i = 0; i < in_ivector.size(); i++)
|
||||
{
|
||||
sum += ivector[i];
|
||||
sum += in_ivector[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
void concat_ivector(std::vector<int>& ivector, const std::vector<int> other_ivector)
|
||||
void concat_ivector(std::vector<int>& inout_ivector, const std::vector<int>& in_ivector)
|
||||
{
|
||||
ivector.insert(ivector.end(), other_ivector.begin(), other_ivector.end());
|
||||
inout_ivector.insert(inout_ivector.end(), in_ivector.begin(), in_ivector.end());
|
||||
}
|
||||
|
||||
// string vectors
|
||||
|
|
@ -53,7 +57,13 @@ std::vector<std::string> create_svector(const int size, const char* value)
|
|||
return std::vector<std::string>(size, value);
|
||||
}
|
||||
|
||||
void concat_svector(std::vector<std::string>& svector, const std::vector<std::string> other_svector)
|
||||
void print_svector(const std::vector<std::string> in_svector)
|
||||
{
|
||||
svector.insert(svector.end(), other_svector.begin(), other_svector.end());
|
||||
std::copy(in_svector.begin(), in_svector.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
|
||||
|
||||
}
|
||||
|
||||
void concat_svector(std::vector<std::string>& inout_svector, const std::vector<std::string>& in_svector)
|
||||
{
|
||||
inout_svector.insert(inout_svector.end(), in_svector.begin(), in_svector.end());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,13 +4,15 @@
|
|||
#include <string>
|
||||
|
||||
std::vector<double> create_dvector(const int size, const double value);
|
||||
double sum_dvector(const std::vector<double> dvector);
|
||||
void concat_dvector(std::vector<double>& dvector, const std::vector<double> other_dvector);
|
||||
double sum_dvector(const std::vector<double> in_vector);
|
||||
void concat_dvector(std::vector<double>& inout_dvector, const std::vector<double>& in_dvector);
|
||||
|
||||
std::vector<int> create_ivector(const int size, const int value);
|
||||
int sum_ivector(const std::vector<int> ivector);
|
||||
void concat_ivector(std::vector<int>& ivector, const std::vector<int> other_ivector);
|
||||
int sum_ivector(const std::vector<int> in_ivector);
|
||||
void concat_ivector(std::vector<int>& inout_ivector, const std::vector<int>& in_ivector);
|
||||
|
||||
std::vector<std::string> create_svector(const int size, const char* value);
|
||||
void concat_svector(std::vector<std::string>& svector, const std::vector<std::string> other_svector);
|
||||
void print_svector(const std::vector<std::string> in_svector);
|
||||
void concat_svector(std::vector<std::string>& inout_svector, const std::vector<std::string>& in_svector);
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -8,4 +8,8 @@
|
|||
|
||||
%include "std_vector.i"
|
||||
|
||||
%include "example.hxx";
|
||||
%apply std::vector<double> &INOUT { std::vector<double>& inout_dvector };
|
||||
%apply std::vector<int> &INOUT { std::vector<int>& inout_ivector };
|
||||
%apply std::vector<std::string> &INOUT { std::vector<std::string>& inout_svector };
|
||||
|
||||
%include "example.hxx"
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ disp(iv3);
|
|||
disp("now an example with vectors of string.");
|
||||
disp("create the vector of string {''aa'', ''aa''}:");
|
||||
sv = create_svector(2, "aa");
|
||||
disp(sv);
|
||||
print_svector(sv);
|
||||
sv2 = create_svector(2, "bb");
|
||||
disp("concat this vector with the vector of string {''bb'', ''bb''}:");
|
||||
sv3 = concat_svector(sv, sv2);
|
||||
disp(sv3);
|
||||
print_svector(sv3);
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue