Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. STL vector, search for a sequence

STL vector, search for a sequence

Scheduled Pinned Locked Moved ATL / WTL / STL
c++csharpjavagraphicsregex
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kaminem
    wrote on last edited by
    #1

    Hi guys, I have two vectors of string and I'm looking for an easy way to find the first vector sequence or sub-sequence into the first one for example: Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "C++", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 3 and match length 2 Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "JAVA", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 0 and match length 1 thanks for any idea, suggestion

    S S 2 Replies Last reply
    0
    • K kaminem

      Hi guys, I have two vectors of string and I'm looking for an easy way to find the first vector sequence or sub-sequence into the first one for example: Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "C++", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 3 and match length 2 Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "JAVA", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 0 and match length 1 thanks for any idea, suggestion

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      The simplest algorithm is probably this:

      std::pair<size_t, size_t> find_first_subsequence(std::vectorstd::string const& v1,
      std::vectorstd::string const& v2)
      {
      std::vectorstd::string::const_iterator pos = std::find(v1.begin(), v1.end(), v2[0]);
      const size_t firstPos = pos-v1.begin();
      if (pos == v1.end()) return std::make_pair(-1, -1);
      size_t numEqual = 0;
      while(numEqual < v2.size() && pos != v1.end() && *pos == v2[numEqual])
      {
      ++numEqual;
      ++pos;
      }
      return std::make_pair(firstPos, numEqual);
      }

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      1 Reply Last reply
      0
      • K kaminem

        Hi guys, I have two vectors of string and I'm looking for an easy way to find the first vector sequence or sub-sequence into the first one for example: Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "C++", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 3 and match length 2 Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "JAVA", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 0 and match length 1 thanks for any idea, suggestion

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        Why not use std::search[^]?

        Steve

        S 1 Reply Last reply
        0
        • S Stephen Hewitt

          Why not use std::search[^]?

          Steve

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Because std::search only returns a match if it finds the whole of a sequence you're looking for, not a partial match, which is what the OP was looking for.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          S 1 Reply Last reply
          0
          • S Stuart Dootson

            Because std::search only returns a match if it finds the whole of a sequence you're looking for, not a partial match, which is what the OP was looking for.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            Fair point. I didn't read the OP's post carefully enough.

            Steve

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups