File - Studymaterial4IT

advertisement
Demonstrating the generic find algorithm with a vector
#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
using namespace std;
// For find
int main()
{
char x[5] = {'a', 'b', 'c', 'd', 'f'};
vector<char> vector1(&x[0], &x[5]);
// Search for the first occurrence
vector<char>::iterator where = find(vector1.begin(), vector1.end(), 'b');
cout << *where
return 0;
<< endl;
}
b
Use std::copy to output the elements in a vector
#include <iostream>
using std::cout;
using std::endl;
#include
#include
#include
#include
<vector>
<algorithm>
<iterator>
<stdexcept>
//
//
//
//
vector class-template definition
copy algorithm
ostream_iterator iterator
out_of_range exception
int main()
{
int array[ 6 ] = { 1, 2, 3, 4, 5, 6 };
std::vector< int > integers( array, array + 6 );
std::ostream_iterator< int > output( cout, " " );
integers.push_back( 2 );
integers.push_back( 3 );
integers.push_back( 4 );
cout << "Vector integers contains: ";
std::copy( integers.begin(), integers.end(), output );
return 0;
}
Vector integers contains: 1 2 3 4 5 6 2 3 4
Reverse all elements in a vector by using reverse
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v;
unsigned int i;
for(i=0; i<10; i++) v.push_back(i);
cout << "Initial: ";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
cout << endl;
reverse(v.begin(), v.end());
cout << "Reversed: ";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
return 0;
}
Initial: 0 1 2 3 4 5 6 7 8 9
Reversed: 9 8 7 6 5 4 3 2 1 0
Use for_each function to print all elements in a vector
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// function that prints the passed argument
void print (int elem)
{
cout << elem << ' ';
}
int main()
{
vector<int> coll;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}
// print all elements
for_each (coll.begin(), coll.end(),
print);
cout << endl;
// range
// operation
}
1 2 3 4 5 6 7 8 9
insert and erase.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char>
vector<char>
char str[] =
unsigned int
v(10);
v2;
"<Vector>";
i;
for(i=0; i<10; i++) v[i] = i ;
// copy characters in str into v2
for(i=0; str[i]; i++)
v2.push_back(str[i]);
// display original contents of vector
cout << "Original contents of v:\n";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
cout << "\n\n";
vector<char>::iterator p = v.begin();
p += 2; // point to 3rd element
// insert 10 X's into v
v.insert(p, 10, 'X');
// display contents after insertion
cout << "Size after inserting X's = " << v.size() << endl;
cout << "Contents after insert:\n";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
cout << "\n\n";
// remove those elements
p = v.begin();
p += 2; // point to 3rd element
v.erase(p, p+10); // remove next 10 elements
// display contents after deletion
cout << "Size after erase = " << v.size() << endl;
cout << "Contents after erase:\n";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
cout << "\n\n";
// Insert v2 into v
v.insert(p, v2.begin(), v2.end());
cout << "Size after v2's insertion = ";
cout << v.size() << endl;
cout << "Contents after insert:\n";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
cout << endl;
return 0;
}
Assign one string to another
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1("A");
string str2("B");
string str3("G");
string str4;
cout << "
cout << "
cout << "
str1: " << str1 << endl;
str2: " << str2 << endl;
str3: " << str3 << "\n\n";
// Assign one string to another.
str4 = str1;
cout << "str4 after being assigned str1: " << str4 << "\n\n";
return 0;
}
Remove a word with find() and erase
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string strA("This is a test.");
// Create an iterator to a string.
string::iterator itr;
cout << "Remove ' larger'.\n";
itr = find(strA.begin(), strA.end(), 'l');
strA.erase(itr, itr+7);
cout << strA << "\n\n";
return 0;
}
Concatenate two string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1("A");
string str2("B");
string str3("G");
string str4;
cout << "
cout << "
cout << "
str1: " << str1 << endl;
str2: " << str2 << endl;
str3: " << str3 << "\n\n";
// Concatenate two strings.
str4 = str1 + str3;
cout << "str4 after begin assigned st1+str3: " << str4 << "\n\n";
return 0;
}
string.find(substring)
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
string string1( "This is a test string!");
int location;
cout << "Original string:\n" << string1
<< "\n\n(find) \"is\" was found at: " << string1.find( "is" );
return 0;
}
Original string:
This is a test string!
(find) "is" was found at: 2"
search a sub string
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string s1 ="this is a test.";
string s2;
i = s1.find("is");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is:\n";
s2.assign(s1, i, s1.size());
cout << s2;
}
cout << "\n\n";
return 0;
}
Match found at 2
Remaining string is:
is is a test.
Use find the search an element in deque
#include <iostream>
#include <cassert>
#include <deque>
#include <algorithm>
using namespace std;
// For find
int main()
{
int x[5] = {1, 2, 3, 4, 5};
deque<int> deque1(&x[0], &x[5]);
// Search for the first occurrence
deque<int>::iterator where = find(deque1.begin(), deque1.end(), 1);
cout << *where << endl;
return 0;
}
1
Search for first occurrence of deque's contents as a subsequence of the vector contents
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
int main()
{
vector<int> vector1(20);
deque<int> deque1(5);
int i;
for (i = 0; i < 20; ++i)
vector1[i] = i;
for (i = 0; i < 5; ++i)
deque1[i] = i + 5;
vector<int>::iterator k = search(vector1.begin(), vector1.end(), deque1.beg
in(), deque1.end());
for (i = 0; i < 5; ++i)
cout << *(k + i);
return 0;
}
56789
Use reverse function on deque
#include
#include
#include
#include
<iostream>
<cassert>
<string>
<deque>
#include <algorithm> // for reverse
using namespace std;
int main()
{
string s("abcdef");
deque<char> deque1(s.begin(), s.end());
deque<char>::iterator i;
for (i = deque1.begin(); i != deque1.end(); ++i)
cout << *i;
cout << "\n\n\n";
reverse(deque1.begin(), deque1.end());
for (i = deque1.begin(); i != deque1.end(); ++i)
cout << *i;
return 0;
}
abcdef
fedcba
Change size to four elements
#include <iostream>
#include <deque>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
// create empty deque of strings
deque<string> coll;
// insert several elements
coll.assign (3, string("string"));
coll.push_back ("last string");
coll.push_front ("BBB string");
coll.push_front ("CCC string");
coll.push_front ("AAA string");
// print elements separated by newlines
copy (coll.begin(), coll.end(),ostream_iterator<string>(cout,"\n"));
cout << endl;
// change size to four elements
coll.resize (4, "resized string");
// print elements separated by newlines
copy (coll.begin(), coll.end(),ostream_iterator<string>(cout,"\n"));
cout << endl;
}
AAA string
CCC string
BBB string
string
string
string
last string
AAA string
CCC string
BBB string
string
Change all value in a map and output all elements
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
/* create map / associative array
* - keys are strings
* - values are floats
*/
typedef map<string,float> StringFloatMap;
StringFloatMap stocks;
// insert some elements
stocks["BASF"] = 369.50;
// create empty container
stocks["VW"] = 413.50;
stocks["Daimler"] = 819.00;
stocks["BMW"] = 834.00;
stocks["Siemens"] = 842.20;
// print all elements
StringFloatMap::iterator pos;
for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
cout << "stock: " << pos->first << "\t"
<< "price: " << pos->second << endl;
}
cout << endl;
// boom (all prices doubled)
for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
pos->second *= 2;
}
// print all elements
for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
cout << "stock: " << pos->first << "\t"
<< "price: " << pos->second << endl;
}
cout << endl;
}
stock:
stock:
stock:
stock:
stock:
BASF
BMW
Daimler
Siemens
VW
price:
price:
price:
price:
price:
369.5
834
819
842.2
413.5
stock:
stock:
stock:
stock:
stock:
BASF
BMW
Daimler
Siemens
VW
price:
price:
price:
price:
price:
739
1668
1638
1684.4
827
Download