Skip to content

Instantly share code, notes, and snippets.

@chchwy
Created September 15, 2016 04:53
Show Gist options
  • Save chchwy/f0925f871d75f9eb4d1bc8d76e143e71 to your computer and use it in GitHub Desktop.
Save chchwy/f0925f871d75f9eb4d1bc8d76e143e71 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <set>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
bool startsWith( const std::string& s, const std::string& target)
{
if (s.size() < target.size())
{
return false;
}
for (int i = 0; i < target.size(); ++i)
{
if (s[i] != target[i])
{
return false;
}
}
return true;
}
int main(int argc, char *argv[])
{
std::ifstream fin("C:\\Users\\Matthew\\Desktop\\ctex.txt");
std::string line;
//std::cout << "Test" << std::endl;
std::cout << fin.good() << std::endl;
std::vector< std::string > create_set;
std::vector< std::string > destroy_set;
std::map< std::string, int > create_map;
std::map< std::string, int > destroy_map;
while (getline(fin, line))
{
if (!startsWith(line, "CMtlK"))
continue;
std::cout << line << std::endl;
std::istringstream sin(line);
std::string ctex;
std::string type;
int num;
int id;
std::string address;
sin >> ctex >> type >> num >> address;
if (type == "create:")
{
create_set.push_back(address);
create_map.emplace(address, num);
}
else if (type == "destroy:")
{
destroy_set.push_back(address);
destroy_map.emplace(address, num);
}
std::cout << ctex << type << num << address << std::endl;
}
std::sort(create_set.begin(), create_set.end());
std::sort(destroy_set.begin(), destroy_set.end());
std::cout << "Create tex=" << create_set.size() << std::endl;
std::cout << "Destroy tex=" << destroy_set.size() << std::endl;
std::vector< std::string > diff( 500 );
auto it = std::set_difference( create_set.begin(), create_set.end(),
destroy_set.begin(), destroy_set.end(),
diff.begin() );
diff.resize(it - diff.begin());
std::ofstream fout( "C:\\Users\\Matthew\\Desktop\\otex.txt" );
std::map< int, std::string > diff_map;
for (std::string s : diff)
{
int number = create_map.find( s )->second;
diff_map.emplace(number, s);
}
for (auto pair : diff_map)
{
int number = pair.first;
std::string s = pair.second;
std::cout << "Diff=" << number << ":" << s << std::endl;
fout << "Diff=" << number << ":" << s << std::endl;
}
std::cout << "Diff count=" << diff.size() << std::endl;
fout << "Diff count=" << diff.size() << std::endl;
fout.close();
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment