Operation on Objects
This section covers the basic operations you can perform on objects, such as adding, querying, updating, and deleting. We'll use vectors and tables as examples to illustrate these actions.
Vector Operations
The following operations are performed on the previously created vector v1 of type DOUBLE.
Appending Elements
// append a single element
v1->append(new Double(1.5)); // v1: [1.1,2.2,3.3,1.5]
// append 3 elements
v1->append(new Double(2), 3); // v1: [1.1,2.2,3.3,1.5,2,2,2]
Accessing Elements
ConstantSP e1 = v1->get(0); // obtain the first element
double e2 = v1->getDouble(1); // obtain the second element
Updating Vectors
// update the second element with value 100
v1->set(1, new Double(100)); // vi: [1.1,100,3.3,1.5,2,2,2]
Deleting Elements
// remove element in specific position
ConstantSP index = Util::createIndexVector(3, 1);
v1->remove(index); vi: [1.1,100,3.3,2,2,2]
// remove the last 4 elements
v1->remove(4); // [1.1,100]
Tables Operations
The following operations are performed on the previously created table tb1.
Accessing Tables by Column
// obtain the first column
VectorSP col1 = tb1->getColumn(0); // [0,1,2,3,4,5,6,7,8,9]
// obtain the column val
VectorSP col2 = tb1->getColumn("val"); //[0,1.1,2.2,3.3,4.400000000000001,5.5,6.600000000000001,7.700000000000001,8.8,9.9]
Accessing Tables by Row
// obtain the first row
DictionarySP row = tb1->get(0);
std::cout << row->getString() << std::endl;
/* output:
id->0
val->0
*/
Appending Records
INDEX insertedRows;
std::string errorMsg;
VectorSP col0 = Util::createVector(DT_INT, 0);
VectorSP col1 = Util::createVector(DT_DOUBLE, 0);
col0->append(new Int(20));
col1->append(new Double(30.5));
vector<ConstantSP> values{col0, col1};
tb1->append(values, insertedRows, errorMsg);
std::cout << tb1->getString() << std::endl;
/* output:
id val
-- -----------------
0 0
1 1.1
2 2.2
3 3.3
4 4.400000000000001
5 5.5
6 6.600000000000001
7 7.700000000000001
8 8.8
9 9.9
20 30.5
*/
Updating Records
VectorSP col1 = tb1->getColumn(0);
VectorSP col2 = tb1->getColumn("val");
// update the third row by updating the third element of each column
col1->set(2, new Int(100));
col2->set(2, new Double(32.597));
std::cout << tb1->getString() << std::endl;
/* output:
id val
--- ------------------
0 0
1 1.1
100 32.597000000000001
3 3.3
4 4.400000000000001
5 5.5
6 6.600000000000001
7 7.700000000000001
8 8.8
9 9.9
*/
