Skip to content

Instantly share code, notes, and snippets.

@liuguiyangnwpu
Last active August 2, 2016 07:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuguiyangnwpu/3201060ce1eb1026a06a76582a8fc261 to your computer and use it in GitHub Desktop.
Save liuguiyangnwpu/3201060ce1eb1026a06a76582a8fc261 to your computer and use it in GitHub Desktop.
How to write a class with STL
class Widget { ... };
vector<Widget> vw;
Widget bestWidget;
vector<Widget>::iterator it =find(vw.begin(), vw.end(), bestWidget);

you should write this

class Widget { ... };
typedef vector<Widget> WidgetContainer;
typedef WidgetContanier::iterator WCIterator;

WidgetContainer vw;
Widget bestWidget;
WCIterator it = find(vw.begin(), vw.end(), bestWidget);

how to fill the data into the containers

int data[numsValues];
vector<int> nums;
nums.insert(nums.begin(), data, data+numsValues);

you can use iterators

vector<int>::iterator insertLoc(nums.begin());
for(int i=0;i<numsValues;++i) {
	insertLoc = v.insert(insertLoc, nums[i]);
}

we can replace the loop with a call to copy or assign

copy(data, data+numsValues, insert(nums, nums.begin()));
nums.assign(data, data+numsValues);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment