CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 5 of 5

Thread: a question regarding to std vector

  1. #1
    Join Date
    Nov 2011

    a question regarding to std vector

    I have a vector defined as follows

     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".
     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?


  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: a question regarding to std vector

    Yes, as an example consider

    #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 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++17 Compiler: Microsoft VS2019 (16.7.4)

  3. #3
    Join Date
    Oct 2008

    Re: a question regarding to std vector

    FYI, c++11 added std::any_of to replace the more verbose std::find_if() != .end()

  4. #4
    Join Date
    Jun 2015

    Re: a question regarding to std vector

    Quote Originally Posted by laephy View Post
    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.

  5. #5
    Join Date
    May 2007

    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)