SWIG c++ vector access in python
Question: This may be a noob question but here it goes. I have wrapped a 3d vector into
a python module using SWIG. Everything has compiled and I can import the
module and perform actions with it. I can't seem to figure out how to access
my vector in python to store and change values in it. How do I store and
change my vector values in python. My code is below and was written to test if
the algorithm stl works with SWIG. It does seem to work but I need to be able
to put values into my vector with python.

header.h

    
    
     #ifndef HEADER_H_INCLUDED
    
     #define HEADER_H_INCLUDED
    
     #include <vector>
    
     using namespace std;
    
     struct myStruct{
    
     int vecd1, vecd2, vecd3;
    
     vector<vector<vector<double> > >vec3d;
    
     void vecSizer();
     void deleteDuplicates();
     double vecSize();
     void run();
     };
    
     #endif // HEADER_H_INCLUDED
    

main.cpp

    
    
     #include "header.h"
     #include <vector>
     #include <algorithm>
    
     void myStruct::vecSizer()
     {
         vec3d.resize(vecd1);
    
         for(int i = 0; i < vec3d.size(); i++)
         {
             vec3d[i].resize(vecd2);
    
             for(int j = 0; j < vec3d[i].size(); j++)
             {
                 vec3d[i][j].resize(vecd3);
             }
         }
     }
    
     void myStruct::deleteDuplicates()
     {
         vector<vector<vector<double> > >::iterator it;
         sort(vec3d.begin(),vec3d.end());
         it = unique(vec3d.begin(),vec3d.end());
         vec3d.resize(distance(vec3d.begin(), it));
     }
    
     double myStruct::vecSize()
     {
         return vec3d.size();
     }
    
     void myStruct::run()
     {
         vecSizer();
         deleteDuplicates();
         vecSize();
     }
    

from the terminal (Ubuntu)

    
    
     import test #import the SWIG generated module
     x = test.myStruct() #create an instance of myStruct
     x.vecSize() #run vecSize() should be 0 since vector dimensions are not initialized
     0.0 
     x.vec3d #see if vec3d exists and is of the correct type
     <Swig Object of type 'vector< vector< vector< double > > > *' at       0x7fe6a483c8d0>
    

Thanks in advance!


Answer: It turns out that vectors are converted to immutable python objects when the
wrapper/interface is generated. So in short you cannot modify wrapped c++
vectors from python.

