Tuesday, December 30, 2008
Sunday, December 28, 2008
Container Summary Complexity from StrousStroup
Vector
[] - const
List Operations - O(n)+
Iterators - Random
List
List operation - const
Front operations - const
Back operations - const
Iterators - Bi
Dequeue
[] - const
List operations - O(n)
Front operations - const
Back operations - const
Iterators - Ran
List operations include
- Insert
- erase
- clear
[] - const
List Operations - O(n)+
Iterators - Random
List
List operation - const
Front operations - const
Back operations - const
Iterators - Bi
Dequeue
[] - const
List operations - O(n)
Front operations - const
Back operations - const
Iterators - Ran
List operations include
- Insert
- erase
- clear
Saturday, December 27, 2008
StrousStroup Container Design : Review
Notes from Container Design Chapter in StrousStroup
- What is a container?
A Container is an object that holds other objects
- What is an iterator?
Iterator is a means of traversing objects in the container (Used to navigate containers)
- Important member of a Container for iterator
iterator begin() //points to the first element
const_iterator begin() const
iterator end() //points to one-past-last element
const_iterator end() const
reverse_iterator rbegin()
const_reverse_iterator ebegin() const;
reverse_iterator end()
const_reverse_iterator rend() const;
- Element access in a vector
reference operator[] (size_type n); //unchecked access
reference at (size_type n); //checked access
- Passing vectors
f1(vector &)
f2(const vector &)
vector v(1000);
f1(v) //pass reference
- reserve in vector allocates space in advance
- capacity() gives the current numbers of reserved memory slots
- size() gives the current number of elements
- Pointer cannot address a unit of memory smaller than a byte
- Link
Vector Code
- What is a container?
A Container is an object that holds other objects
- What is an iterator?
Iterator is a means of traversing objects in the container (Used to navigate containers)
- Important member of a Container for iterator
iterator begin() //points to the first element
const_iterator begin() const
iterator end() //points to one-past-last element
const_iterator end() const
reverse_iterator rbegin()
const_reverse_iterator ebegin() const;
reverse_iterator end()
const_reverse_iterator rend() const;
- Element access in a vector
reference operator[] (size_type n); //unchecked access
reference at (size_type n); //checked access
- Passing vectors
f1(vector
f2(const vector
vector
f1(v) //pass reference
- reserve in vector allocates space in advance
- capacity() gives the current numbers of reserved memory slots
- size() gives the current number of elements
- Pointer cannot address a unit of memory smaller than a byte
- Link
Vector Code
Thursday, December 25, 2008
Wednesday, December 24, 2008
C++ Abstract Base Classes
#include "string"
#include "sstream"
using namespace std;
class AbstractStatic {
public:
static std::string constructKey(int a, int b) {
std::ostringstream ostr;
ostr «« a «« ":" «« b «« endl;
return ostr.str();
}
virtual std::string makekey(int a, int b) = 0;
virtual ~AbstractStatic();
};
class AbstractConcrete: public AbstractStatic
{
public:
std::string makeKey(int a, int b) {
std::ostringstream ostr;
ostr «« a «« "+" «« b «« endl;
return ostr.str();
}
};
void foo(AbstractStatic* a) {
cout «« "In function foo" «« endl;
}
int main(int argc, char* argv[]) {
AbstractStatic* as;
AbstractStatic as; //this is an erro and foo(as) is also an error
string a = AbstractConcrete::constructKey(1,2);
foo(as);
cout «« a «« endl;
return 0;
}
#include "sstream"
using namespace std;
class AbstractStatic {
public:
static std::string constructKey(int a, int b) {
std::ostringstream ostr;
ostr «« a «« ":" «« b «« endl;
return ostr.str();
}
virtual std::string makekey(int a, int b) = 0;
virtual ~AbstractStatic();
};
class AbstractConcrete: public AbstractStatic
{
public:
std::string makeKey(int a, int b) {
std::ostringstream ostr;
ostr «« a «« "+" «« b «« endl;
return ostr.str();
}
};
void foo(AbstractStatic* a) {
cout «« "In function foo" «« endl;
}
int main(int argc, char* argv[]) {
AbstractStatic* as;
AbstractStatic as; //this is an erro and foo(as) is also an error
string a = AbstractConcrete::constructKey(1,2);
foo(as);
cout «« a «« endl;
return 0;
}
Saturday, December 20, 2008
STL MAP
From deez.info
"
Let’s say you have two STL std::maps with identical types, and you want to copy all the elements from one to the other. The easiest way to do this is to use map::insert():
typedef std::map map_t;
map_t map1;
map_t map2;
// Copy all elements from map1 to map2
map2.insert(map1.begin(), map1.end());
Alternatively, you could use the STL std::copy algorithm:
// Copy all elements from map1 to map2.
std::copy(map1.begin(), map1.end(), std::inserter(map2, map2.begin()));
Both methods’ performance should be an amortized O(n) because they insert records in sorted order and use the hinting form of map::insert.
Note that because both methods ultimately call map::insert they will not overwrite a preexisting key’s associated value. In other words, if map1 has the value V1 associated with key K and map2 the value V2 associated with the same key K, V2 will remain in map2 after the copy operation.
Let’s say you want to perform the copy but have map1’s values overwrite map2’s for identical keys. The first way to solve this problem that entered my mind was to write my own OutputIterator which performs an overwriting assignment and pass it to std::copy. However, there’s a far simpler approach. You can copy map2’s values into map1, relying on the fact that map2’s values won’t overwrite map1’s, and then swap the results:
map1.insert(map2.begin(), map2.end());
map2.swap(map1);
Thanks, Sam, for helping me figure all this out.
"
"
Let’s say you have two STL std::maps with identical types, and you want to copy all the elements from one to the other. The easiest way to do this is to use map::insert():
typedef std::map
map_t map1;
map_t map2;
// Copy all elements from map1 to map2
map2.insert(map1.begin(), map1.end());
Alternatively, you could use the STL std::copy algorithm:
// Copy all elements from map1 to map2.
std::copy(map1.begin(), map1.end(), std::inserter(map2, map2.begin()));
Both methods’ performance should be an amortized O(n) because they insert records in sorted order and use the hinting form of map::insert.
Note that because both methods ultimately call map::insert they will not overwrite a preexisting key’s associated value. In other words, if map1 has the value V1 associated with key K and map2 the value V2 associated with the same key K, V2 will remain in map2 after the copy operation.
Let’s say you want to perform the copy but have map1’s values overwrite map2’s for identical keys. The first way to solve this problem that entered my mind was to write my own OutputIterator which performs an overwriting assignment and pass it to std::copy. However, there’s a far simpler approach. You can copy map2’s values into map1, relying on the fact that map2’s values won’t overwrite map1’s, and then swap the results:
map1.insert(map2.begin(), map2.end());
map2.swap(map1);
Thanks, Sam, for helping me figure all this out.
"
Subscribe to:
Posts (Atom)