2.2.3.18 vector
Name
vector
Remark
An Origin C vector is a dynamically allocated and sized array that is not tied to an internal Origin data set allowing a greater degree of flexibility.
vector is a template class with a default type of double but a vector of any basic data type (including char, byte, short, word, int, uint, complex, string) can be constructed using the syntax vector<type>. The vector class is derived from the vectorbase class from which it inherits methods and properties.
Origin 2027 supports vector of simple struct. See EX3 in Example below.
Hierarchy
- vectorbase
- vector
Examples
EX1
// Example for numeric type vector void vector_ex1() { int imax = 10; vector<double> vX(imax); // vector of doubles having imax elements vector vY(imax); // vector of doubles by default vector<int> vI; // vector of ints for(int ii = 0; ii < vX.GetSize(); ii++) vX[ii] = rnd(); vY = 10 * vX; // vector arithmetic is supported vI = vY; // Cast double vector into int vector for(ii = 0; ii < vI.GetSize(); ii++) // Output cast elements of vectors printf("vI[%d]=%d after cast from %f\n",ii,vI[ii],vY[ii]); }
EX2
// StringArray Example // StringArray is typedef vector<string> in string.h void vector_ex2() { // declare one StringArray variable named saList and assign values StringArray saList = { "Hello", "World", "!"}; for(int ii = 0; ii < saList.GetSize(); ii++) // Output string one by one printf("%s\n", saList[ii]); vector<string> vsNames(3); // set the vector's size in construct // assign string value vsNames[0] = "Tom"; vsNames[1] = "Jenny"; vsNames[2] = "Joy"; for(int jj = 0; jj < vsNames.GetSize(); jj++) // Output string one by one printf("%s\n", vsNames[jj]); }
EX3
//vector of simple struct struct StSimpleTypes { int nValue; double rValue; }; void vector_struct_simple_types() { vector<StSimpleTypes> vst; StSimpleTypes st1; st1.nValue = 100; st1.rValue = 1000.88; vst.Add(st1); st1.nValue = 101; st1.rValue = 1001.88; vst.Add(st1); st1.nValue = 102; st1.rValue = 1002.88; vst.Add(st1); StSimpleTypes *pst = vst; out_int("pst[2].nValue = ", pst[2].nValue); out_double("pst[2].rValue = ", pst[2].rValue); StSimpleTypes *pst2; pst2 = pst; pst2 += 2; pst2->nValue = 666; pst2->rValue = 777.4; out_int("AFTER: pst[2].nValue = ", pst[2].nValue); out_double("AFTER: pst[2].rValue = ", pst[2].rValue); vst[2].nValue = 13; vst[2].rValue = 33.44; out_int("AFTER2: pst[2].nValue = ", pst[2].nValue); out_double("AFTER2: pst[2].rValue = ", pst[2].rValue); double *pr2 = &pst[2].rValue; out_double("*pr2 = ", *pr2); *pr2 = 66.2; out_double("*pr2 = ", *pr2); StSimpleTypes *pst3 = &vst[1]; pst3->nValue = -99; pst3->rValue = -99.345; out_int("AFTER3:vst[1].nValue = ", vst[1].nValue); out_double("AFTER3: vst[1].rValue = ", vst[1].rValue); StSimpleTypes st4; st4.nValue = 111; st4.rValue = 1111.11; vst.InsertAt(1, st4); out_int("AFTER4:vst[1].nValue = ", vst[1].nValue); out_double("AFTER4: vst[1].rValue = ", vst[1].rValue); out_int("AFTER4:vst[3].nValue = ", vst[3].nValue); out_double("AFTER4: vst[3].rValue = ", vst[3].rValue); out_int("size=", vst.GetSize()); vst.RemoveAt(0); vector<StSimpleTypes> vst2; StSimpleTypes st2; st2.nValue = 1; st2.rValue = 10.88; vst2.Add(st2); st2.nValue = 11; st2.rValue = 11.88; vst2.Add(st2); vst.Append(vst2); }
Header to Include
origin.h
Reference
Members
| Name | Brief | Example |
|---|---|---|
| Add | Adds One element at the end of the vector. | Examples |
| Find | Find a specified string in a string vector. | Examples |
| GetSourceRange | gets the range inside the source data when the constructor vector(Column& cc, int nLowerIndex = -1, int nUpperIndex = -1, int nWriteback = WRITEBACK_NO) is used. See that constructor and the accompanying example for more details. | Examples |
| InsertAt | Inserts one element, or multiple copies of an element at a specified index in a vector. In the process, by incrementing the index, it shifts up the existing element at this index, and it shifts up all the elements above it. | Examples |
| InsertValue | It inserts the same value at the multiple locations inside a vector. The vector itself is resized as needed. | Examples |
| Pattern | Fill vector with arbitrary set of text values | |
| RemoveAt | Removes one or more elements starting at a specified index in a vector. It can removes elements at specified indices in a vector also. | Examples |
| SetAtGrow | Sets the vector element at the specified index. | Examples |
| vector | Constructors for vector class. | Examples |