Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::string arr[5] = { "EVEN", "ODD", "NONE", "MARK", "SPACE" };
- #include <stdio.h>
- #include <algorithm>
- #include <string>
- using std::string;
- std::string arr[5] = { "EVEN", "ODD", "NONE", "MARK", "SPACE" };
- int main() {
- string* pArrEnd = arr + sizeof( arr)/sizeof(arr[0]);
- string* pFound = std::find( arr, pArrEnd, "MARK");
- if (pFound == pArrEnd) {
- printf( "not foundn");
- }
- else {
- printf( "%s was found at index %dn", pFound->c_str(), pFound - arr);
- printf( "or using STL: %dn", std::distance( arr, pFound));
- }
- return 0;
- }
- // alloc the array
- static const size_t numItems = 100000;
- int * items = new int[numItems];
- // fill the array
- for( size_t n = 0; n < numItems; ++n )
- items[n] = n;
- // find 42 using std::find()
- int* found = std::find(&items[0], &items[numItems], 42);
- if( found == &items[numItems] )
- {
- // this is one past the end, so 42 was not found
- items[0] = 42;
- }
- else
- {
- // we found the first instance of 42 at this location
- // change it to 43
- *found = 43;
- }
- int main()
- {
- // for c++ vector
- typedef int Element;
- typedef std::vector<Element> CppVector;
- CppVector v;
- v.push_back( 2 );
- v.push_back( 4 );
- v.push_back( 8 );
- v.push_back( 6 );
- const Element el = 4;
- CppVector::const_iterator it = std::find( v.begin(), v.end(), el );
- if ( it == v.end() )
- {
- std::cout << "there is no such element" << std::endl;
- }
- else
- {
- const CppVector::size_type index = it - v.begin();
- std::cout << "index = " << index << std::endl;
- }
- // for C array
- typedef Element CVector[4];
- CVector cv;
- cv[0] = 2;
- cv[1] = 4;
- cv[2] = 8;
- cv[3] = 6;
- const std::size_t cvSize = sizeof( cv ) / sizeof( Element );
- std::cout << "c vector size = " << cvSize << std::endl;
- const Element* cit = std::find( cv, cv + cvSize, el );
- const std::size_t index = cit - cv;
- if ( index >= cvSize )
- std::cout << "there is no such element" << std::endl;
- else
- std::cout << "index = " << index << std::endl;
- }
Add Comment
Please, Sign In to add comment