简体   繁体   中英

How to insert an integer vector value into char vector

My function needs to output a value of 'a2l1p1h1a2' for an input of 'alpha'. However, I am facing issues inserting an integer value (from integer vector) into a char vector. Below is the code snippet. Any advice/ suggestion/ observation is very appreciated!

void stringCharCount(string inStr)
{
    unsigned int i = 0, j = 0, count = 0;
    vector<char> charVect;
    vector<int> charCount;
    vector<char> charnCount;

    vector<char> :: iterator itr1;
    vector<int> :: iterator itr2;
    vector<char> :: iterator itr3;

    string inpStr = inStr;

    for(i = 0; i < inpStr.length(); i++)
    {
        //Push each character of the string into the vector
        charVect.push_back(inpStr.at(i));

        // Count the number of occurrences of each character in the string
        for(j = 0 ; j < inpStr.length(); j++)
        {
            if(inpStr.at(i) == inpStr.at(j))
            {
                count++;
            }
        }
        charCount.push_back(count);
        count = 0; // Reset for counting the next character's count
    }

    for(itr1 = charVect.begin(), itr2 = charCount.begin(); itr1 != charVect.end(), itr2 != charCount.end(); itr1++, itr2++)
    {
        cout << "Char: " << *itr1 << " ";
        cout << "Char count: " << *itr2 << endl;
        charnCount.push_back(*itr1);
        char tempChar = static_cast<char> (*itr2);
        charnCount.push_back(tempChar);  // THIS LINE DOES NOT SEEM TO WORK!!
        cout << "Char count: " << *itr2 << endl;
    }

    for(itr3 = charnCount.begin(); itr3 != charnCount.end(); itr3++)
    {
        cout << "Char, its count: " << *itr3 << endl;
    }
}
  1. You don't need charVect and inpStr to store extra copies of inStr .
  2. Seems like string is better than vector<char> for charnCount , since the count may exceed 128, and it's much simpler to append to string . If you print (char)1 , it will print nothing instead of '1' , because 1 means start of heading in ASCII, and '1' is actually 49. You have to do what @tadman said in comment, or convert it to string using std::to_string .
  3. Use ranged-based-for loop instead of iterator. It's more clearer. It is also applicapable to string .
  4. Useemplace_back instead of push_back for vector , which is faster. (But for std::vector<int> , there isn't any different. However, I recommend using emplace_back instead of push_back if vector contains other objects)
#include <iostream>
#include <vector>
#include <string>

using namespace std;

void stringCharCount(const string& inStr)
{
    vector<int> charCount;
    string charnCount;

    // I wouldn't touch the algorithm, but there is a better way to avoid  duplicated checks
    for(const auto& ch : inStr) {
        int count = 0;
        for(const auto& ch2 : inStr)
            if(ch == ch2) ++count;

        charCount.emplace_back(count); 
    }

    for(int i=0; i<inStr.length(); ++i){
        cout << "Char: " << inStr[i] << " ";
        cout << "Char count: " << charCount[i] << endl;

        charnCount += inStr[i];
        charnCount += to_string(charCount[i]);
    }


    for(const auto& ch : charnCount){
        cout << "Char, its count: " << ch << endl;
    }

    cout << charnCount << endl;
}

int main(int argc, char** argv){

    stringCharCount("alpha");

    return 0;
}

I would suggest counting the characters first (using a map is easiest), and then building the output by using a stringstream; no back and forth insertion required:

std::map<char, int> counts;

for (char ch : inStr)
    ++counts[ch];

std::ostringstream out;

for (char ch : inStr)
    out << ch << counts[ch];

std::cout << out.str() << std::endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM