- Converting string to int: to convert a string to an integer in
C++
(before C++11
), there are two typical ways:
#include <iostream>
#include <sstream>
#include <cstdlib> // for atoi()
int main()
{
std::string s = "1234";
std::string t = "4567";
int i = atoi(s.c_str());
std::cout << "i = " << i << std::endl;
std::istringstream iss(s);
int j;
iss >> j;
std::cout << "j = " << j << std::endl;
iss.clear(); // clear previous stream
iss.str(t); // set t to be characters in the new stream
int k;
iss >> k;
std::cout << "k = " << k << std::endl;
return 0;
}
- Edges as pairs of integers: the best way to store edges of a graph is to treat each edge as a
pair
of integers. C++ has a pair
type that you can use. (here are some examples on generic usage of pair.)
// from this example, you can see that pairs are compared lexicographically
#include <iostream>
int main()
{
std::pair<int, int> p1; // p1 is a pair of ints
std::pair<int, int> p2; // p2 is also a pair of ints
std::pair<int, int> p3; // p3 is also a pair of ints
p1 = std::make_pair(1, 5);
p2 = std::make_pair(5, 1);
p3 = std::make_pair(1, 5);
std::cout << "p1 " << (p1 == p2? "=" : "not =") << " p2" << std::endl;
std::cout << "p1 " << (p1 < p2? "<" : "not <") << " p2" << std::endl;
std::cout << "p1 " << (p1 == p3? "=" : "not =") << " p3" << std::endl;
std::cout << "p1 " << (p1 < p3? "=" : "not <") << " p3" << std::endl;
return 0;
}
- Inserting elements into a set. set is one of the most straightforward data structures to use.
#include <iostream>
#include <set>
int main()
{
std::pair<int, int> p1;
std::pair<int, int> p2;
std::pair<int, int> p3;
std::pair<int, int> p4;
p1 = std::make_pair(1, 5);
p2 = std::make_pair(5, 1);
p3 = std::make_pair(1, 5);
p4 = std::make_pair(2, 3);
std::set<std::pair<int, int> > edgeSet;
edgeSet.insert(p1);
edgeSet.insert(p2);
edgeSet.insert(p3);
edgeSet.insert(p4);
// the following prints "# of inserted edges = 3", do you see why?
std::cout << "# of inserted edges = " << edgeSet.size() << std::endl;
return 0;
}
- Sort, traverse a vector, and the erase function:
#include <iostream>
#include <vector>
#include <algorithm> // for sort()
using namespace std; // I'm lazy now, so let's get rid of all the std::
void printVector(vector<pair<int, int> > & myVec)
{
// traverse the vector
vector<pair<int, int> >::iterator i;
for (i = myVec.begin(); i != myVec.end(); ++i) {
cout << "(" << i->first << ", " << i->second << ") ";
}
cout << endl;
}
int main()
{
pair<int, int> p1;
pair<int, int> p2;
pair<int, int> p3;
pair<int, int> p4;
p1 = make_pair(2, 4);
p2 = make_pair(5, 1);
p3 = make_pair(2, 4);
p4 = make_pair(4, 2);
vector<pair<int, int> > pairVector;
pairVector.push_back(p1);
pairVector.push_back(p2);
pairVector.push_back(p3);
pairVector.push_back(p4);
cout << "# of inserted pairs = " << pairVector.size() << endl;
printVector(pairVector);
sort(pairVector.begin(), pairVector.end());
printVector(pairVector);
// finally, remove the first duplicate pair that it found
vector<pair<int, int> >::iterator i = pairVector.begin();
while (i != pairVector.end()) {
vector<pair<int, int> >::iterator j = i+1;
if (j != pairVector.end() && *j == *i) {
// remove the pair pointed to by i as a duplicate was found
i = pairVector.erase(i);
break;
}
}
printVector(pairVector);
return 0;
}