enum class color{red,green, blue}; 

there is such an enum, I want to put it in a vector, and then return from the function

 vector<color> getVectorFunc(); 

I do not understand how to do this?

 vector<color> getVectorFunc(){ return //......//; } 
  • And how does this differ from working with a vector, say, from an int ? Alternatively, return {color::red, color::green, color::blue}; . - HolyBlackCat
  • And how do I get a vector out? - user9431986
  • Just as you would get the return value of any other type. For example: std::vector<color> my_vec = getVectorFunc(); . - HolyBlackCat
  • What does it mean "I do not understand how" ??? Enter your return vector in return - that's all. What's the question? - AnT
  • The vector - assumes an "array" (array diversity). Create, fill - return. Those. there ... and what is there? There are three solutions at least. - nick_n_a

2 answers 2

I do not understand where the problem is ?

 #include <vector> enum class color {red,green, blue}; std::vector<color> getVectorFunc() { std::vector<color> v {color::red,color::green, color::blue}; return v; } int main() { std::vector<color> v = getVectorFunc(); } 

    This is how you can put enum in a std :: vector container and return it from a function . Just do not forget to connect the vector library.

     enum color { red, green, blue }; // объявление перечисления std::vector <color> getVectorFunc() // ф-я { std::vector <color> Vec; // создаём вектор принимающий enum color c; // создаём объект Vec.push_back(c); // метод push.back() добавляет переданный объект в конец вектора return Vec; // возвращаем вектор } int main() { std::vector <color> getVectorFunc(); // вызов ф-ии возвращающей enum return 0; } 

    And what was the problem?

    • one
      std::vector <color> getVectorFunc(); // вызов ф-ии возвращающей enum std::vector <color> getVectorFunc(); // вызов ф-ии возвращающей enum Yeah, how is it :) - HolyBlackCat
    • in my opinion, this is not a working version at all - user9431986
    • You have no "call f-ii" in main . - AnT
    • even if so write std :: vector <color> res = getVectorFunc (); Will not work anyway - user9431986
    • one
      “what is the problem?” The problem is that about “into the vector the garbage will fall” it was necessary to write from the very beginning in the question. And give an example of the code in which this happens. We are not telepaths. - HolyBlackCat