Why does the std::find for vector return a iterator instead of the integer positionWhat's the difference...
Is Sanskrit really the mother of all languages?
Why do old games use flashing as means of showing damage?
What is the source of the fear in the Hallow spell's extra Fear effect?
What quests do you need to stop at before you make an enemy of a faction for each faction?
Identifying the following distribution
A magician's sleight of hand
Do I have to rename all creatures in a new world?
How quickly would a wooden treasure chest rot?
What are some countries where you can be imprisoned for reading or owning a Bible?
Tiny image scraper for xkcd.com
If I have an accident, should I file a claim with my car insurance company?
How do I anonymously report the Establishment Clause being broken?
Why does the seven segment display have decimal point at the right?
Shoes for commuting
What is the majority of the UK Government as of 2019-09-04?
Travel to USA with a stuffed puppet
Do 643,000 Americans go bankrupt every year due to medical bills?
'Hard work never hurt anyone' Why not 'hurts'?
A Meal fit for a King
Is the interior of a Bag of Holding actually an extradimensional space?
Professor refuses to write a recommendation letter to students who haven't written a research paper with him
Was "The Hobbit" ever abridged?
Can anyone find an image of Henry Bolingbroke's Sovereygne Feather Seal?
Why don't they build airplanes from 3D printer plastic?
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;
}
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
New contributor
add a comment |
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
New contributor
add a comment |
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
New contributor
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
c++ std
New contributor
New contributor
edited 8 hours ago
Casey
4,6755 gold badges44 silver badges68 bronze badges
4,6755 gold badges44 silver badges68 bronze badges
New contributor
asked 8 hours ago
GaniGani
413 bronze badges
413 bronze badges
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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 frombegin
toend
, replacebegin
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
8 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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 frombegin
toend
, replacebegin
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
8 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
|
show 1 more comment
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.
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 frombegin
toend
, replacebegin
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
8 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
|
show 1 more comment
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.
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.
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 frombegin
toend
, replacebegin
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
8 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
|
show 1 more comment
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 frombegin
toend
, replacebegin
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
8 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
8 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
8 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
|
show 1 more comment
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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