Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1503 - 4
- 1703 - 1a, iteratorer svårt. Men nästan fått kläm på nu
- b, samma här men bättre än innan. Förstå uppg. svåra
- 1706 - 5 småkonstig print10 är svår
- alllmänt templates och overloading av operators
- ostream, istream -operators som friend, copy i ostream etc.
- Bra att kolla igenom:
- ~NameList() {
- for(list_type::iterator it = names.begin(); it != names.end(); it++) {
- delete names[it]; //fel, behöver inte skriva access på det sättet iom vi använder en iterator!
- delete *it; //tar bort vad iteratorn pekar på!
- }
- }
- ostream& BI::operator<<(ostream& os, const BI& b) {
- copy(b.digits.rbegin(), b.digits.rend(), ostream_iterator<int>(cout));
- return os;
- }
- ostream& operator<<(ostream& os, Regent& r) {
- os << r.name << " " << r.succ << " of " << r.nation
- << " " << r.start << "-" << r.end;
- return os;
- }
- istream& operator>>(istream& is, Regent& r) {
- while(is) {
- is >> r.name >> r.nation >> r.start >> r.end >> r.succ;
- }
- return is;
- }
- int main(int ac, char** av) {
- ifstream is(av[1]);
- set<Regent> rulers;
- while(is >> getline()) {
- Regent r(is.line() >> ...);
- rulers.insert(r);
- }
- auto it = rulers.begin();
- while(it != rulers.end()) {
- cout << rulers[i] << endl;
- it++;
- }
- //fel, eftersom de os och is är implementerade gör du såhär istället:
- set<Ruler> rulers((istream_iterator<Ruler>(is), istream_iterator<Ruler>()));
- copy(rulers.begin(), rulers.end(), ostream_iterator<Ruler>(cout, "\n"));
- }
- auto value = *first;
- *res = value; //eller *first
- while(++first != last) {
- auto tmp = *first;
- *++result = tmp - value;
- value = tmp;
- }
- return ++result;
- deque<int> a;
- copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(a));
- copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
- while(a.size() > 1) {
- deque<int> b;
- adj_diff(a.begin(), a.end(), back_inserter(b));
- b.pop_front();
- a.swap(b);
- copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
- }
- ifstream infile("morse.def");
- char symbol;
- string code;
- while(infile >> symbol >> code) {
- table.insert(make_pair(code, symbol));
- }
- infile.close()
- istringstream iss(code);
- string current;
- string s;
- while(iss >> current) {
- auto it = table.find(current);
- ...
- istream& BI::operator<<(istream& is, BI& b) {
- string s;
- is >> s;
- b = BI(s);
- return is;
- }
- template <typename InputIterator>
- pair<int, int> max_succ(InputIterator first, InputIterator last) {
- FEL
- v
- value_type type = iterator_traits<first>::value_type;
- Såhär istället:
- using type = typename itarator_traits<InputIterator>::value_type;
- }
- En move-konstruktor. Copy tar längre tid än move.
- Image(const Image&& rhs) : w(rhs.w), h(rhs.h), pixels(rhs.pixels) {
- rhs.pixels = nullptr;
- }
- int main(int argc, const char** argv) {
- count_words(argv[i], words);
- |
- |----det är en ifstream som passas in i funktionen
- v
- void count_words(istream& in, map<string, size_t> words) {
- if(!in) { cout << "File cannot be opened" << endl; }
- string word;
- while(in >> word) {
- ...
- }
- -------------------------------------------------------------------------------
- typedef map<string, size_t>::const_iterator words_iter_type;
- void countwords(ifstream& in, map<string, size_t> words) {
- string word;
- while(in >> word) {
- words[word]++;
- }
- }
- bool comp_meth(words_iter_type it1, words_iter_type it2) {
- return it1->second > it2->second;
- }
- print10(map<string, size_t> words) {
- vector<words_iter_type> iterators;
- for(auto it = words.begin(); it != words.end(); it++) {
- iterators.push_back(it);
- }
- sort(iterators.begin(), iterators.end(), comp_meth);
- vector<words_iter_type> vec_iter = iterators.begin();
- for(; vec_iter != iterators.end(); vec_iter++) {
- cout << (*vec_iter)->first << " " << (*vec_iter)->second << endl;
- }
- }
- int main(char argc, const char** argv) {
- map<string, size_t> words;
- for(int i = 0; i < argc; i++) {
- ifstream in(argv[i]);
- if(!in) { cout << "file could not be opened" << endl; }
- else {
- countwords(in, words);
- }
- in.close();
- }
- print10(words);
- }
- -------------------------------------------------------------------------------
- template <typename InputIterator, typename OutputIterator, typename T, typename U>
- void apply_all(InputIterator functions_start, InputIterator functions_end,
- OutputIterator result, T& a, U& b) {
- while(functions_start != functions_end) {
- *result = (*functions_start)(a, b); //måste peka på funktionen! functions(a, b) duger ej;
- result++; //måste vara (*functions) för att hämta funktionen
- functions_start++;
- }
- }
- -------------------------------------------------------------------------------
- int* last = remove_copy_if(a, a + SIZE, b,
- [&c](int x) { if(x < c) {
- cout << x << endl;
- }});
- template <typename InIt, typename Pred>
- remove_copy_if(InIt first, InIt last, OutIt res, Pred p) {
- while(first != last) {
- if(! p(*first)) {
- *res++ = *first;
- }
- first++;
- }
- }
- -------------------------------------------------------------------------------
- .h
- class Scheduler {
- public:
- Scheduler(istream& in);
- void schedule();
- void print(const ostream& os) const;
- private:
- usingset_type = bitset<80>;
- bool compare(int row_nbr);
- bool sort_meth(set_type s1, set_type s2);
- vector<set_type> rows;
- }
- .cpp
- Scheduler::Scheduler(ifstream& in) {
- string line;
- while(getline(in, line)) {
- rows.push_back(set_type(val));
- }
- in.close();
- }
- void Scheduler::schedule() {
- sort(rows.begin(), rows.end(), rows.begin(), sort_meth);
- for(size_t it = 1; it != rows.size(); it++) {
- while(collides_with(row_nbr)) {
- rows[i] <<= 1;
- }
- }
- }
- bool Scheduler::compare(set_type row_nbr) {
- for(int i = 0; i != row_nbr; i++) {
- if((rows[i] & rows[row_nbr]).any()) {
- return true;
- }
- }
- return false;
- }
- void Scheduler::print(const ostream& os) const {
- for(auto r : rows) {
- for(auto it = r.begin(); it != r.size(); it++) {
- if(r[it] == 0) cout << " ";
- else cout << 1;
- }
- cout << endl;
- }
- }
- bool Scheduler::sort_meth(set_type s1, set_type s2) {
- return s1.count() > s2.count();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement