#Unexpected Output when printing contents of a vector

16 messages · Page 1 of 1 (latest)

wet templeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

alpine ocean
#

;compile clang_trunk -std=c++23

mellow socketBOT
#
Compiler Output
In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/x86_64-linux-gnu/bits/stdc++.h:53:
In file included from /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/functional:72:
In file included from /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/vector:67:
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/bits/stl_bvector.h:189:35: error: expected string literal as argument of '__assume__' attribute
  189 |       __attribute__ ((__assume__ (__ofst < unsigned(_S_word_bit))));
      |                                   ^
In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/x86_64-linux-gnu/bits/stdc++.h:227:
/opt/compiler-explorer/gcc-snaps
glass delta
#

I may have thought of a potential solution, should I delete the value of multiplicand after I push it into the vector? Because otherwise it would add character onto multiplicand in the next loop. Is this correct?

alpine ocean
#

you are not resetting multiplicand

#

;compile clang_trunk -std=c++23 -stdlib=libc++

#include <iostream>
#include <sstream>
#include <vector>

int main()
{
    std::vector<std::string> expressionTerms {"3*2", "2*2"};
    std::string              multiplicand;
    std::vector<int>         evalVector;

    for (const std::string& term : expressionTerms)
    {
        std::cout << "checking string " << term << '\n';
        for (char character : term)
        {
            if (character != '*')
            {
                multiplicand += character;
                std::cout << "  Not *, adds " << character << ", now: " << multiplicand << '\n';
            }
            else
            {
                std::stringstream ss {multiplicand};
                int               val {};
                ss >> val;
                std::cout << "  is *, adds " << val << " to eval vector\n";
                evalVector.push_back(val);
            }
        }
        // evalVector.push_back(stoi(multiplicand));

        for (int i : evalVector)
        {
            std::cout << i << '\n';
        }
    }


    return 0;
}
mellow socketBOT
#
Program Output
checking string 3*2
  Not *, adds 3, now: 3
  is *, adds 3 to eval vector
  Not *, adds 2, now: 32
3
checking string 2*2
  Not *, adds 2, now: 322
  is *, adds 322 to eval vector
  Not *, adds 2, now: 3222
3
322
glass delta
#

!solved

wet templeBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

alpine ocean
#

you are also printing inside the loop

#

@glass delta consider using ranges. std::views::split could be helpful.

#

;compile clang_trunk -std=c++23 -stdlib=libc++

#include <iostream>
#include <sstream>
#include <vector>

int main()
{
    std::vector<std::string> expressionTerms {"4*2", "2*2"};
    std::vector<int>         evalVector;

    for (const std::string& term : expressionTerms)
    {
        std::string              multiplicand{};
        std::cout << "checking string " << term << '\n';
        std::stringstream ss {};
        int               val {};
        for (char character : term)
        {
            if (character != '*')
            {
                multiplicand += character;
                std::cout << "  Not *, adds " << character << ", now: " << multiplicand << '\n';
            }
            else
            {
                ss = std::stringstream{std::move(multiplicand)};
                ss >> val;
                std::cout << "  is *, adds " << val << " to eval vector\n";
                evalVector.push_back(val);
                multiplicand.clear();
            }
        }
        ss = std::stringstream{std::move(multiplicand)};
        ss >> val;
        std::cout << "  is eol, adds " << val << " to eval vector\n";
        evalVector.push_back(val);

    }
    for (int i : evalVector)
    {
        std::cout << i << '\n';
    }


    return 0;
}
mellow socketBOT
#
Program Output
checking string 4*2
  Not *, adds 4, now: 4
  is *, adds 4 to eval vector
  Not *, adds 2, now: 2
  is eol, adds 2 to eval vector
checking string 2*2
  Not *, adds 2, now: 2
  is *, adds 2 to eval vector
  Not *, adds 2, now: 2
  is eol, adds 2 to ev
glass delta
alpine ocean
wet templeBOT
#

<@undefined>

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.