Search code examples
c++stdstdstring

How to iterate over std::string const& in a function?


I am passing a const reference std::string to a function. I want to iterate over the triples of the string and convert those triples to ints. I do so by bit shifting. My problem is that I do not know how to iterate over the string as I need the element and the index oof the triple in the string.

#include <vector>
#include <string>

unsigned int three_char_to_int(std::string start){
    unsigned int substr {0};

    substr |= start[0];
    substr |= (start[1] << 8);
    substr |= (start[2] << 16);

    return substr;
}

std::vector<int> test_Function(std::string const& text){

    int length = text.length();

    std::vector<unsigned int> res = std::vector<unsigned int>(length, 0);

    for(int i {0}; i < length-2; ++i){
        continue;

        // text is not a pointer, so this does not work
        // res[i] = three_char_to_int(text +i);

        // this does not work either
        // res[i] = three_char_to_int(text[i]);
    }

}

int main(){

    std::string text = {"Hello, World!"};
    auto result = test_Function(text);

    // do something with result

    return 0;
} 

I'd be thankful for any hints and solutions. If you have a better idea how to do what I'm trying I'll be happy to see that, too.

Note: I'm learning C++


Solution

  • Assuming you are not interested in refactoring your code considerably to use c++20 or c++23 features, try utilizing std::vector::emplace_back and std::string::substr:

    #include <iostream>
    #include <string>
    #include <vector>
    
    /**
     * @brief Converts a 3-character string to an unsigned int.
     *
     * @param start The string to be converted must be at least 3 characters long.
     * @return unsigned int The resulting unsigned int from the conversion.
     */
    unsigned int three_char_to_int(const std::string& start) {
      unsigned int substr = 0;
      substr |= static_cast<unsigned char>(start[0]);
      substr |= static_cast<unsigned char>(start[1]) << 8;
      substr |= static_cast<unsigned char>(start[2]) << 16;
      return substr;
    }
    
    /**
     * @brief Processes a string to convert each consecutive triple of characters into an unsigned int.
     *
     * @param text The const reference to the input string to be processed.
     * @return std::vector<unsigned int> A vector containing the converted unsigned ints.
     */
    std::vector<unsigned int> test_Function(const std::string& text) {
      std::vector<unsigned int> res;
      res.reserve(text.length() - 2); // Code go brrr 🏎️...
      for (size_t i = 0; i + 2 < text.length(); ++i) {
        res.emplace_back(three_char_to_int(text.substr(i, 3)));
      }
      return res;
    }
    
    int main() {
      std::string text = "Hello, World!";
      std::vector<unsigned int> result = test_Function(text);
      for (auto& i : result) {
        std::cout << i << '\n';
      }
      return 0;
    }
    

    Output:

    7103816
    7105637
    7302252
    2912108
    2108527
    5709868
    7296800
    7499607
    7107183
    6581362
    2188396