Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. 1503 - 4
  2.  
  3. 1703 - 1a, iteratorer svårt. Men nästan fått kläm på nu
  4. b, samma här men bättre än innan. Förstå uppg. svåra
  5.  
  6. 1706 - 5 småkonstig print10 är svår
  7.  
  8.  
  9.  
  10.  
  11.  
  12. alllmänt templates och overloading av operators
  13.  
  14. ostream, istream -operators som friend, copy i ostream etc.
  15.  
  16.  
  17.  
  18.  
  19. Bra att kolla igenom:
  20.  
  21.  
  22. ~NameList() {
  23. for(list_type::iterator it = names.begin(); it != names.end(); it++) {
  24. delete names[it]; //fel, behöver inte skriva access på det sättet iom vi använder en iterator!
  25.  
  26. delete *it; //tar bort vad iteratorn pekar på!
  27. }
  28. }
  29.  
  30.  
  31.  
  32.  
  33. ostream& BI::operator<<(ostream& os, const BI& b) {
  34. copy(b.digits.rbegin(), b.digits.rend(), ostream_iterator<int>(cout));
  35. return os;
  36. }
  37.  
  38.  
  39.  
  40. ostream& operator<<(ostream& os, Regent& r) {
  41. os << r.name << " " << r.succ << " of " << r.nation
  42. << " " << r.start << "-" << r.end;
  43. return os;
  44. }
  45.  
  46. istream& operator>>(istream& is, Regent& r) {
  47. while(is) {
  48. is >> r.name >> r.nation >> r.start >> r.end >> r.succ;
  49. }
  50. return is;
  51. }
  52.  
  53. int main(int ac, char** av) {
  54. ifstream is(av[1]);
  55.  
  56. set<Regent> rulers;
  57. while(is >> getline()) {
  58. Regent r(is.line() >> ...);
  59. rulers.insert(r);
  60. }
  61. auto it = rulers.begin();
  62. while(it != rulers.end()) {
  63. cout << rulers[i] << endl;
  64. it++;
  65. }
  66.  
  67. //fel, eftersom de os och is är implementerade gör du såhär istället:
  68.  
  69. set<Ruler> rulers((istream_iterator<Ruler>(is), istream_iterator<Ruler>()));
  70. copy(rulers.begin(), rulers.end(), ostream_iterator<Ruler>(cout, "\n"));
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77. auto value = *first;
  78. *res = value; //eller *first
  79. while(++first != last) {
  80. auto tmp = *first;
  81. *++result = tmp - value;
  82. value = tmp;
  83. }
  84. return ++result;
  85.  
  86.  
  87.  
  88.  
  89. deque<int> a;
  90. copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(a));
  91. copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
  92. while(a.size() > 1) {
  93. deque<int> b;
  94. adj_diff(a.begin(), a.end(), back_inserter(b));
  95. b.pop_front();
  96. a.swap(b);
  97. copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
  98. }
  99.  
  100.  
  101.  
  102. ifstream infile("morse.def");
  103. char symbol;
  104. string code;
  105. while(infile >> symbol >> code) {
  106. table.insert(make_pair(code, symbol));
  107. }
  108. infile.close()
  109.  
  110.  
  111.  
  112.  
  113. istringstream iss(code);
  114. string current;
  115. string s;
  116. while(iss >> current) {
  117. auto it = table.find(current);
  118. ...
  119.  
  120.  
  121.  
  122. istream& BI::operator<<(istream& is, BI& b) {
  123. string s;
  124. is >> s;
  125. b = BI(s);
  126. return is;
  127. }
  128.  
  129.  
  130.  
  131.  
  132. template <typename InputIterator>
  133. pair<int, int> max_succ(InputIterator first, InputIterator last) {
  134.  
  135. FEL
  136. v
  137. value_type type = iterator_traits<first>::value_type;
  138.  
  139. Såhär istället:
  140. using type = typename itarator_traits<InputIterator>::value_type;
  141. }
  142.  
  143.  
  144.  
  145.  
  146. En move-konstruktor. Copy tar längre tid än move.
  147.  
  148. Image(const Image&& rhs) : w(rhs.w), h(rhs.h), pixels(rhs.pixels) {
  149. rhs.pixels = nullptr;
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. int main(int argc, const char** argv) {
  158. count_words(argv[i], words);
  159. |
  160. |----det är en ifstream som passas in i funktionen
  161. v
  162. void count_words(istream& in, map<string, size_t> words) {
  163. if(!in) { cout << "File cannot be opened" << endl; }
  164. string word;
  165. while(in >> word) {
  166. ...
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. -------------------------------------------------------------------------------
  180.  
  181.  
  182. typedef map<string, size_t>::const_iterator words_iter_type;
  183.  
  184. void countwords(ifstream& in, map<string, size_t> words) {
  185. string word;
  186. while(in >> word) {
  187. words[word]++;
  188. }
  189. }
  190.  
  191. bool comp_meth(words_iter_type it1, words_iter_type it2) {
  192. return it1->second > it2->second;
  193. }
  194.  
  195. print10(map<string, size_t> words) {
  196. vector<words_iter_type> iterators;
  197. for(auto it = words.begin(); it != words.end(); it++) {
  198. iterators.push_back(it);
  199. }
  200. sort(iterators.begin(), iterators.end(), comp_meth);
  201. vector<words_iter_type> vec_iter = iterators.begin();
  202. for(; vec_iter != iterators.end(); vec_iter++) {
  203. cout << (*vec_iter)->first << " " << (*vec_iter)->second << endl;
  204. }
  205. }
  206.  
  207. int main(char argc, const char** argv) {
  208. map<string, size_t> words;
  209. for(int i = 0; i < argc; i++) {
  210. ifstream in(argv[i]);
  211. if(!in) { cout << "file could not be opened" << endl; }
  212. else {
  213. countwords(in, words);
  214. }
  215. in.close();
  216. }
  217. print10(words);
  218. }
  219.  
  220. -------------------------------------------------------------------------------
  221.  
  222. template <typename InputIterator, typename OutputIterator, typename T, typename U>
  223. void apply_all(InputIterator functions_start, InputIterator functions_end,
  224. OutputIterator result, T& a, U& b) {
  225. while(functions_start != functions_end) {
  226. *result = (*functions_start)(a, b); //måste peka på funktionen! functions(a, b) duger ej;
  227. result++; //måste vara (*functions) för att hämta funktionen
  228. functions_start++;
  229. }
  230. }
  231.  
  232. -------------------------------------------------------------------------------
  233.  
  234.  
  235. int* last = remove_copy_if(a, a + SIZE, b,
  236. [&c](int x) { if(x < c) {
  237. cout << x << endl;
  238. }});
  239.  
  240. template <typename InIt, typename Pred>
  241. remove_copy_if(InIt first, InIt last, OutIt res, Pred p) {
  242. while(first != last) {
  243. if(! p(*first)) {
  244. *res++ = *first;
  245. }
  246. first++;
  247. }
  248. }
  249.  
  250.  
  251. -------------------------------------------------------------------------------
  252.  
  253.  
  254. .h
  255.  
  256.  
  257. class Scheduler {
  258. public:
  259. Scheduler(istream& in);
  260. void schedule();
  261. void print(const ostream& os) const;
  262. private:
  263. usingset_type = bitset<80>;
  264. bool compare(int row_nbr);
  265. bool sort_meth(set_type s1, set_type s2);
  266. vector<set_type> rows;
  267. }
  268.  
  269. .cpp
  270.  
  271. Scheduler::Scheduler(ifstream& in) {
  272. string line;
  273. while(getline(in, line)) {
  274. rows.push_back(set_type(val));
  275. }
  276. in.close();
  277. }
  278.  
  279. void Scheduler::schedule() {
  280. sort(rows.begin(), rows.end(), rows.begin(), sort_meth);
  281. for(size_t it = 1; it != rows.size(); it++) {
  282. while(collides_with(row_nbr)) {
  283. rows[i] <<= 1;
  284. }
  285. }
  286. }
  287.  
  288. bool Scheduler::compare(set_type row_nbr) {
  289. for(int i = 0; i != row_nbr; i++) {
  290. if((rows[i] & rows[row_nbr]).any()) {
  291. return true;
  292. }
  293. }
  294. return false;
  295. }
  296.  
  297. void Scheduler::print(const ostream& os) const {
  298. for(auto r : rows) {
  299. for(auto it = r.begin(); it != r.size(); it++) {
  300. if(r[it] == 0) cout << " ";
  301. else cout << 1;
  302. }
  303. cout << endl;
  304. }
  305. }
  306.  
  307.  
  308. bool Scheduler::sort_meth(set_type s1, set_type s2) {
  309. return s1.count() > s2.count();
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement