Answers for "Iterator in c++"

C++
0

string iterator in c++

// string::begin/end
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
    std::cout << *it << endl;
  std::cout << '\n';

  return 0;
}
Posted by: Guest on November-21-2020
0

Iterator in c++

//Iterator Pointer like Structore in c++ of STL
#include <bits/stdc++.h>
using namespace std;

int main(){
    vector<int> v = {1,2,3,4,5};
    for(int i = 0; i<v.size(); i++){
        cout<<v[i]<<" ";
    }
    cout<<endl;

    vector<int> :: iterator it;
    // it = v.begin();
    // cout<<(*it+1)<<endl;

    for(it = v.begin(); it !=v.end(); ++it){
        cout<<(*it)<<endl;
    }
    cout<<endl;

    vector<pair<int, int>> v_p = {{1,2},{3,4},{5,6}};
    vector<pair<int ,int>> :: iterator iter;
    for(iter = v_p.begin(); iter !=v_p.end(); ++iter){
        cout<<(*iter).first<<" "<<(*iter).second<<endl;
    }
    cout<<endl;
    for(iter = v_p.begin(); iter !=v_p.end(); ++iter){
        cout<<(iter->first)<<" "<<(iter->second)<<endl;
    }

    return 0;
}
Posted by: Guest on September-13-2021
0

iterator on std::tuple

#include <tuple>
#include <iostream>
#include <boost/hana.hpp>
#include <boost/hana/ext/std/tuple.hpp>

struct Foo1 {
    int foo() const { return 42; }
};

struct Foo2 {
    int bar = 0;
    int foo() { bar = 24; return bar; }
};

int main() {
    using namespace std;
    using boost::hana::for_each;

    Foo1 foo1;
    Foo2 foo2;

    for_each(tie(foo1, foo2), [](auto &foo) {
        cout << foo.foo() << endl;
    });

    cout << "foo2.bar after mutation: " << foo2.bar << endl;
}
Posted by: Guest on November-10-2020
0

declaring iterator in cpp

vector<int>::iterator ptr;
Posted by: Guest on June-12-2020

Browse Popular Code Answers by Language