-
July 1st, 2015, 04:04 PM
#1
a question regarding to std vector
I have a vector defined as follows
Code:
vector<wstring> m_vInfo;
which contains information about the calculation. For each calculation iteration, an element is added to the vector through the function push_back. These elements can contain both strings "completed calculation" or "error during calculation".
Code:
m_vInfo = {completed calculation, error during calculation, completed calculation, completed, completed calculation, error during calculation, ..................}
My question is if there is a function to know if an element contains for example the word completed or not?
Thanks
-
July 1st, 2015, 04:57 PM
#2
Re: a question regarding to std vector
Yes, as an example consider
Code:
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
const string tofind{ "completed" };
const vector<string> vs{ "error during calculation", "completed calculation", "calculation", "error during calculation" };
cout << "Does" << ((find_if(vs.begin(), vs.end(), [&tofind](string elem){return elem.find(tofind) != string::npos; }) == vs.end()) ? " not" : "") << " contain " << tofind << endl;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 10th, 2015, 08:40 AM
#3
Re: a question regarding to std vector
FYI, c++11 added std::any_of to replace the more verbose std::find_if() != .end()
-
July 10th, 2015, 02:34 PM
#4
Re: a question regarding to std vector
Originally Posted by laephy
My question is if there is a function to know if an element contains for example the word completed or not?
I think you should consider it a two-stage problem.
1. The first problem is to visit each element of the vector.
2. The second problem is to find out something about each element.
If you separate your code in this way you have the most options available to you. Both stages can be performed using available library functions or higher level language constructs.
For example you can do the visiting (1) in parallel utilizing all cores of the computer. If this is done in a performance bottleneck it may greatly improve the throughtput of your program.
Last edited by tiliavirga; July 12th, 2015 at 04:58 AM.
-
July 13th, 2015, 08:46 AM
#5
Re: a question regarding to std vector
Learning aside, perhaps the wrong tool for the job is being used in your here. If the only two options are that the task passed or that it failed, then wouldn't a container of boolean values (e.g. deque<bool> to avoid the bad vector<bool> specialisation) be a better option here (e.g. completed = true, failed = false)? Else if more than two options are possible then a vector of enumerations would be better. Just my two cents.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|