Why does the std::find for vector return a iterator instead of the integer positionWhat's the difference between “STL” and “C++ Standard Library”?Why isn't sizeof for a struct equal to the sum of sizeof of each member?What should main() return in C and C++?Iteration over std::vector: unsigned vs signed index variableHow to find out if an item is present in a std::vector?Why is “using namespace std;” considered bad practice?Why should C++ programmers minimize use of 'new'?Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognitionfind in std::vector<std::pair>Efficient way to return a std::vector in c++

Undefined Hamiltonian for this particular Lagrangian

Which costing factors go into the optimizer choosing different types of spools?

Is it rude to ask my opponent to resign an online game when they have a lost endgame?

Numerical minimum of a one-valued function

What's the point of this macro?

Is mathematics truth?

Bidirectional Dictionary

How can I implement regular expressions on an embedded device?

How does the UK House of Commons think they can prolong the deadline of Brexit?

My Friend James

Life post thesis submission is terrifying - Help!

Do 643,000 Americans go bankrupt every year due to medical bills?

Why did Boris Johnson call for new elections?

Low quality postdoc application and deadline extension

If magnetic force can't do any work, then how can we define a potential?

Left my gmail logged in when I was fired

Do I have to rename all creatures in a new world?

If I have an accident, should I file a claim with my car insurance company?

Is it risky to move from broad geographical diversification into investing mostly in less developed markets?

Shoes for commuting

Tiny image scraper for xkcd.com

Label "Alto en grasa saturada, sal, ..." should there also be Alta?

ASCII Maze Rendering 3000

What's this constructed number's starter?



Why does the std::find for vector return a iterator instead of the integer position


What's the difference between “STL” and “C++ Standard Library”?Why isn't sizeof for a struct equal to the sum of sizeof of each member?What should main() return in C and C++?Iteration over std::vector: unsigned vs signed index variableHow to find out if an item is present in a std::vector?Why is “using namespace std;” considered bad practice?Why should C++ programmers minimize use of 'new'?Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognitionfind in std::vector<std::pair>Efficient way to return a std::vector in c++






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7















Why does the vector find return the iterator instead of the integer value?



vector<string>::iterator itr1 = std::find(words.begin(), words.end(), word);









share|improve this question









New contributor



Gani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



























    7















    Why does the vector find return the iterator instead of the integer value?



    vector<string>::iterator itr1 = std::find(words.begin(), words.end(), word);









    share|improve this question









    New contributor



    Gani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      7












      7








      7


      1






      Why does the vector find return the iterator instead of the integer value?



      vector<string>::iterator itr1 = std::find(words.begin(), words.end(), word);









      share|improve this question









      New contributor



      Gani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Why does the vector find return the iterator instead of the integer value?



      vector<string>::iterator itr1 = std::find(words.begin(), words.end(), word);






      c++ std






      share|improve this question









      New contributor



      Gani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question









      New contributor



      Gani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question








      edited 8 hours ago









      Casey

      4,6755 gold badges44 silver badges68 bronze badges




      4,6755 gold badges44 silver badges68 bronze badges






      New contributor



      Gani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked 8 hours ago









      GaniGani

      413 bronze badges




      413 bronze badges




      New contributor



      Gani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      Gani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          1 Answer
          1






          active

          oldest

          votes


















          12
















          std::find works for all sorts of containers, not just std::vector. For example, it works with std::list but that container does not allow accessing elements by index (at least not easily). For it to work with all kinds of containers it needs to return something all containers understand, an iterator.



          Edit : If you want to find the index position equivalent to a given iterator, you can use std::distance. For example :



          std::distance(std::begin(words), itr1);



          This will work for standard containers but it may be slower for some. It returns the size of the container if the element is not found, since find returns end if it fails to find the element and the distance between begin and end is the size of the container.






          share|improve this answer



























          • Is it possible to find all the indexes of occurrences of the word in the vector?

            – Gani
            8 hours ago






          • 2





            @Gani Yes, perform the search in a loop. Instead of searching from begin to end, replace begin by the iterator following the last one you found. I don't think the standard library has a function for that.

            – François Andrieux
            8 hours ago







          • 1





            @Gani: Why do you want indices so badly? What's wrong with using iterators?

            – Nicol Bolas
            8 hours ago






          • 2





            Iterators are the glue that holds containers and algorithms together. I've seen several 3rd party collection libraries, and none of them had iterators. They also didn't have algorithms because they (we) couldn't get them to work with enough containers without 'em.

            – Mark Storer
            7 hours ago











          • @NicolBolas I am working on some algorithm which needs all the indexes to be stored in the vector. Its easy to code for it but I was just curious if the STL library has something for me.

            – Gani
            4 hours ago










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Gani is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57794072%2fwhy-does-the-stdfind-for-vector-return-a-iterator-instead-of-the-integer-posit%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          12
















          std::find works for all sorts of containers, not just std::vector. For example, it works with std::list but that container does not allow accessing elements by index (at least not easily). For it to work with all kinds of containers it needs to return something all containers understand, an iterator.



          Edit : If you want to find the index position equivalent to a given iterator, you can use std::distance. For example :



          std::distance(std::begin(words), itr1);



          This will work for standard containers but it may be slower for some. It returns the size of the container if the element is not found, since find returns end if it fails to find the element and the distance between begin and end is the size of the container.






          share|improve this answer



























          • Is it possible to find all the indexes of occurrences of the word in the vector?

            – Gani
            8 hours ago






          • 2





            @Gani Yes, perform the search in a loop. Instead of searching from begin to end, replace begin by the iterator following the last one you found. I don't think the standard library has a function for that.

            – François Andrieux
            8 hours ago







          • 1





            @Gani: Why do you want indices so badly? What's wrong with using iterators?

            – Nicol Bolas
            8 hours ago






          • 2





            Iterators are the glue that holds containers and algorithms together. I've seen several 3rd party collection libraries, and none of them had iterators. They also didn't have algorithms because they (we) couldn't get them to work with enough containers without 'em.

            – Mark Storer
            7 hours ago











          • @NicolBolas I am working on some algorithm which needs all the indexes to be stored in the vector. Its easy to code for it but I was just curious if the STL library has something for me.

            – Gani
            4 hours ago















          12
















          std::find works for all sorts of containers, not just std::vector. For example, it works with std::list but that container does not allow accessing elements by index (at least not easily). For it to work with all kinds of containers it needs to return something all containers understand, an iterator.



          Edit : If you want to find the index position equivalent to a given iterator, you can use std::distance. For example :



          std::distance(std::begin(words), itr1);



          This will work for standard containers but it may be slower for some. It returns the size of the container if the element is not found, since find returns end if it fails to find the element and the distance between begin and end is the size of the container.






          share|improve this answer



























          • Is it possible to find all the indexes of occurrences of the word in the vector?

            – Gani
            8 hours ago






          • 2





            @Gani Yes, perform the search in a loop. Instead of searching from begin to end, replace begin by the iterator following the last one you found. I don't think the standard library has a function for that.

            – François Andrieux
            8 hours ago







          • 1





            @Gani: Why do you want indices so badly? What's wrong with using iterators?

            – Nicol Bolas
            8 hours ago






          • 2





            Iterators are the glue that holds containers and algorithms together. I've seen several 3rd party collection libraries, and none of them had iterators. They also didn't have algorithms because they (we) couldn't get them to work with enough containers without 'em.

            – Mark Storer
            7 hours ago











          • @NicolBolas I am working on some algorithm which needs all the indexes to be stored in the vector. Its easy to code for it but I was just curious if the STL library has something for me.

            – Gani
            4 hours ago













          12














          12










          12









          std::find works for all sorts of containers, not just std::vector. For example, it works with std::list but that container does not allow accessing elements by index (at least not easily). For it to work with all kinds of containers it needs to return something all containers understand, an iterator.



          Edit : If you want to find the index position equivalent to a given iterator, you can use std::distance. For example :



          std::distance(std::begin(words), itr1);



          This will work for standard containers but it may be slower for some. It returns the size of the container if the element is not found, since find returns end if it fails to find the element and the distance between begin and end is the size of the container.






          share|improve this answer















          std::find works for all sorts of containers, not just std::vector. For example, it works with std::list but that container does not allow accessing elements by index (at least not easily). For it to work with all kinds of containers it needs to return something all containers understand, an iterator.



          Edit : If you want to find the index position equivalent to a given iterator, you can use std::distance. For example :



          std::distance(std::begin(words), itr1);



          This will work for standard containers but it may be slower for some. It returns the size of the container if the element is not found, since find returns end if it fails to find the element and the distance between begin and end is the size of the container.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 8 hours ago

























          answered 8 hours ago









          François AndrieuxFrançois Andrieux

          17.9k3 gold badges31 silver badges52 bronze badges




          17.9k3 gold badges31 silver badges52 bronze badges















          • Is it possible to find all the indexes of occurrences of the word in the vector?

            – Gani
            8 hours ago






          • 2





            @Gani Yes, perform the search in a loop. Instead of searching from begin to end, replace begin by the iterator following the last one you found. I don't think the standard library has a function for that.

            – François Andrieux
            8 hours ago







          • 1





            @Gani: Why do you want indices so badly? What's wrong with using iterators?

            – Nicol Bolas
            8 hours ago






          • 2





            Iterators are the glue that holds containers and algorithms together. I've seen several 3rd party collection libraries, and none of them had iterators. They also didn't have algorithms because they (we) couldn't get them to work with enough containers without 'em.

            – Mark Storer
            7 hours ago











          • @NicolBolas I am working on some algorithm which needs all the indexes to be stored in the vector. Its easy to code for it but I was just curious if the STL library has something for me.

            – Gani
            4 hours ago

















          • Is it possible to find all the indexes of occurrences of the word in the vector?

            – Gani
            8 hours ago






          • 2





            @Gani Yes, perform the search in a loop. Instead of searching from begin to end, replace begin by the iterator following the last one you found. I don't think the standard library has a function for that.

            – François Andrieux
            8 hours ago







          • 1





            @Gani: Why do you want indices so badly? What's wrong with using iterators?

            – Nicol Bolas
            8 hours ago






          • 2





            Iterators are the glue that holds containers and algorithms together. I've seen several 3rd party collection libraries, and none of them had iterators. They also didn't have algorithms because they (we) couldn't get them to work with enough containers without 'em.

            – Mark Storer
            7 hours ago











          • @NicolBolas I am working on some algorithm which needs all the indexes to be stored in the vector. Its easy to code for it but I was just curious if the STL library has something for me.

            – Gani
            4 hours ago
















          Is it possible to find all the indexes of occurrences of the word in the vector?

          – Gani
          8 hours ago





          Is it possible to find all the indexes of occurrences of the word in the vector?

          – Gani
          8 hours ago




          2




          2





          @Gani Yes, perform the search in a loop. Instead of searching from begin to end, replace begin by the iterator following the last one you found. I don't think the standard library has a function for that.

          – François Andrieux
          8 hours ago






          @Gani Yes, perform the search in a loop. Instead of searching from begin to end, replace begin by the iterator following the last one you found. I don't think the standard library has a function for that.

          – François Andrieux
          8 hours ago





          1




          1





          @Gani: Why do you want indices so badly? What's wrong with using iterators?

          – Nicol Bolas
          8 hours ago





          @Gani: Why do you want indices so badly? What's wrong with using iterators?

          – Nicol Bolas
          8 hours ago




          2




          2





          Iterators are the glue that holds containers and algorithms together. I've seen several 3rd party collection libraries, and none of them had iterators. They also didn't have algorithms because they (we) couldn't get them to work with enough containers without 'em.

          – Mark Storer
          7 hours ago





          Iterators are the glue that holds containers and algorithms together. I've seen several 3rd party collection libraries, and none of them had iterators. They also didn't have algorithms because they (we) couldn't get them to work with enough containers without 'em.

          – Mark Storer
          7 hours ago













          @NicolBolas I am working on some algorithm which needs all the indexes to be stored in the vector. Its easy to code for it but I was just curious if the STL library has something for me.

          – Gani
          4 hours ago





          @NicolBolas I am working on some algorithm which needs all the indexes to be stored in the vector. Its easy to code for it but I was just curious if the STL library has something for me.

          – Gani
          4 hours ago








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.












          Gani is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          Gani is a new contributor. Be nice, and check out our Code of Conduct.












          Gani is a new contributor. Be nice, and check out our Code of Conduct.











          Gani is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57794072%2fwhy-does-the-stdfind-for-vector-return-a-iterator-instead-of-the-integer-posit%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

          Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

          19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу