codingdir logo sitemap sitemap |

Using C-style arrays as backend for STL string operations

By : , Category : c++

The definition of std::smatch is a specialization of std::match_results. This specialization uses string::const_iterator as the iterator type in the template arguments passed to std::match_results. This requires the begin and end arguments passed to std::regex_search to also be of type string::const_iterator.

In C++ pointers satisfy the requirements of bidirectional iterators and it is not necessary to wrap them in an iterator class. If you need to search through a buffer pointed to by a char pointer you can either use std::cmatch or use std::match_results and specify the iterator type explicitly. In the following two examples I have retained the use of PointerIterator to provide solutions that directly apply to your current code base. I have also included a stand alone example you can reference in the event you want to eliminate the use of your custom iterator class.

PointerIterator<char> wrapper(first, last);
std::cmatch match; // <<--

while (std::regex_search(wrapper.begin(), wrapper.end(), match, re))
    // do something

...using std::match_results instead.

PointerIterator<char> wrapper(first, last);
std::match_results<const char*> match; // <<--

while (std::regex_search(wrapper.begin(), wrapper.end(), match, re))
    // do something

Below is a stand alone example that should provide a bit of codified clarification. It is based on the example on and uses const char* instead of std::string as the search target.

#include <regex>
#include <iostream>
int main()
    const char *haystack = "Roses are #ff0000";
    const int size = strlen(haystack);

    std::regex pattern(

    std::cmatch results;

    std::regex_search(haystack, haystack + size, results, pattern);

    for (size_t i = 0; i < results.size(); ++i) {
        std::csub_match  sub_match = results[i];
        std::string sub_match_str = sub_match.str();
        std::cout << i << ": " << sub_match_str << '

This produces the following output.

0: #ff0000
1: ff
2: 00
3: 00

ReLated :

If you simply want to convert all Elements from the vehicleList to String-Objects, you can write

String[] result = new String[vehicleList.length];
for (int i = 0; i < vehicleList.length; i++)
    result[i] = vehicleList[i].toString();

return result;

Simply write the code into your getVehicleList()-Method. You might have to add a custom toString()-Method in your Vehicle-Class!

You could just call a method in your select that modifies the text in the manner that you would like.

        static void Main(string[] args)
            var fileListEntries = from line in lines
                                  where !(line.StartsWith("#"))
                                  select ( ModifyString(line));


        private static string[] ModifyString(string line)
            string[] elements = line.Split(';');
            elements[0] = "modifiedString";
            return elements;

This might be a workaround, I'll illustrate on a single row of data:

a={'Non - U.S. Citizen','Denied','M','INDIA','Full Time','E-Other','','','MSME',''}

Sum each cell element, this casts the strings to doubles and sum thier value. It will work assuming the odds for a non unique sum result are slim (if not there's a trick you can implement, but I doubt it'll actually happen):


now you have a single number per cell element, you can use cell2mat to get a matrix and or pdist etc ...

I do recommend use back-end if there are operation on secure data. If not - use front-end, because:

  1. As you mentioned, it's additional load to server and server time
  2. If there is high load, then additionally to previous problem you also will have clogging channel of unneeded requests.

If you can do something without back-end - do it.


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .