简体   繁体   中英

Assign std::vector address of another std::vector

I'm writing a program to balance chemical equations. The program works by taking the equation string, splitting it up into a std::vector with a size of two based upon the equal sign, then parses the left side separatedEquation[0] and the right side separatedEquation[1] into another set of std::vector's leftHalf and rightHalf respectively.

Problem

I have a function Equation::filterEquation that parses the separatedEquation for the element names. I want to use a temporary vector that points to the address of either leftHalf or rightHalf. I know this is probably confusing, but here's my code and what I'mt trying to do. I think I need to use pointers, but I've never had to use pointers before and am not efficient with them.

void Equation::filterEquation()
{
    for(int i=0; i<separatedEquation.size(); i++) //i = index of separated equation
    {
        int index=0;
        std::vector<std::string> equationHalf;
        if(i==0)
            equationHalf = leftHalf; //set equationHalf to the address of leftHalf
        if(i==1)
            equationHalf = rightHalf; //set equationHalf to the address of rightHalf
        for (std::string::iterator it = separatedEquation[i].begin(); it!=separatedEquation[i].end(); ++it, index++)
        {
            //Elements are set up so that He = Helium, while H = Hydrogen. This separates the elements based upon upper and lowercae
            bool UPPER_LETTER = isupper(separatedEquation[i][index]); //true if character is upperCase
            bool NEXT_LOWER_LETTER = islower(separatedEquation[i][index+1]); //true if next is lowerCase
            if (UPPER_LETTER)// if the character is an uppercase letter
            {
                if (NEXT_LOWER_LETTER)
                {
                    std::string temp = separatedEquation[i].substr(index, 2);//add THIS capital and next lowercase
                    equationHalf.push_back(temp); // add temp to vector
                }

                else if (UPPER_LETTER && !NEXT_LOWER_LETTER) //used to try and prevent number from getting in
                {
                    std::string temp = separatedEquation[i].substr(index, i);
                    equationHalf.push_back(temp);
                }
            }
        }
    }

}

In the general sense you would replace:

std::vector<std::string> equationHalf;

...

equationHalf = leftHalf // same for rightHalf

with

std::vector<std::string>* equationHalf;

...

equationHalf = &leftHalf // same for rightHalf

And then replace any instance of equationHalf. with equationHalf-> .

Though, in your case, I might consider seriously reconsidering your design, for instance breaking out the code that operations on equationHalf into a function and passing it a reference to the vector to operate on such as void doStuff(std::vector<std::string> & equationHalf) , then simply calling doStuff(leftHalf) and doStuff(rightHalf) .

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