1

I'd like to simply ask you about something i can't solve in the algorithm I'm designing. I'm asking a question that's purely learning. I searched both stackoverflow and Uncle Google, but I didn't see a logical solution.

 struct MyStruct_1
{
    int number[];
};

Imagine it's such a struct Or imagine that there are a few other things in it that aren't arrays.

 struct MyStruct_2
{
    int  id;
    string ModelName;
    int number[];
};

**Insert ==>>>  ID = 1 ModelName = "ModelA" number = { 6,7,8,9, 57 }
Insert ==>>>  ID = 2 ModelName = "ModelB" number = { 3,17,9,28,8 }
*****************************************************************
Update/Delete ==>>>  ID = 2 ModelName = "ModelB" number = { 9 }
Update/Delete ==>>>  ID = 1 ModelName = "ModelA" number = { 57 }
*****************************************************************
Insert/Add ==>>>   ID = ModelName = "ModelA" number = 103**

In this way, from a structure to a C++ language;

list<MyStruct> demoListe  

when I create

My 1st question: how do we provide data entry? push_back ({ 9,3,65,2 }); I tried it, but I got an error message.

My 2nd question: Can the friend who answers the data to enter the data also sample reading the data?

I'd like to thank everyone who will help and lead me to the right solution.

Regards

3
  • @TedLyngmo What is a viable solution to overcoming prohibitions or complying with prohibitions? Commented Jan 1, 2020 at 20:40
  • What is a viable solution to overcoming prohibitions or complying with prohibitions? Commented Jan 1, 2020 at 20:53
  • I put that in the first sentence of my answer below :-) Use a std::vector instead. Commented Jan 1, 2020 at 20:57

2 Answers 2

2

Note that this code is OK:

struct st
{
   int id;
   string s;
};

vector<st> v;

v.push_back({1, "abc"});

But you can't initialize array with {}, So use Vector Container:

struct st
{
   int id;
   string s;
   vector<int> v;
};

vector<st> u;

u.push_back({1, "abc", vector<int>{1, 1, 2}});
Sign up to request clarification or add additional context in comments.

5 Comments

Can't vector be a solution using just STRUCT and LIST without using it? Thank you very much for the reply
@UmitTerzi Do you mean not to use vector? Of course. Just replace every vector with a list.
Yes, I meant not to use Vector. But without knowing the job, I ask, what do you think is better in terms of performance, which is the solution to do it using lists instead of vector?
@UmitTerzi Which one to choose depends on the use-case. For most use-cases, std::vector is preferred.
@UmitTerzi It depends. Vectors are different from lists. Different works, different capabilities, and different usages. You can see cplusplus.com, Or ask a new question to get more info.
1

Flexible arrays are forbidden in standard C++. You should use a std::vector instead:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
#include <string>

struct MyStruct
{
    int id;
    std::string ModelName;
    std::vector<int> number;
};

int main() {
    // initialized with a few numbers
    MyStruct ms{1, "model", {2,3,4}};

    // push_back a new number
    ms.number.push_back(5);

    // print the numbers
    for(int value : ms.number) {
        std::cout << value << '\n';
    }

    // or make a std::list<MyStruct>
    std::list<MyStruct> demoListe {
        {1, "model_a", {2,3,4}},
        {2, "model_b", {5,6,7,8}}
    };

    // add a model
    demoListe.emplace_back(MyStruct{3, "model_c", {11,22}});

    // add 99 to model_a
    auto ait = std::find_if(demoListe.begin(), demoListe.end(), 
                            [](const MyStruct& ms) { return ms.id==1;} );
    if(ait != demoListe.end())
        ait->number.push_back(99);

    // remove 7 from model_b
    auto bit = std::find_if(demoListe.begin(), demoListe.end(),
                            [](const MyStruct& ms) { return ms.id==2;} );
    if(bit != demoListe.end()) {
        auto seven = std::find(bit->number.begin(), bit->number.end(), 7);
        if(seven != bit->number.end())
            bit->number.erase(seven);
    }

    // print result
    for(const MyStruct& ms : demoListe) {
        std::cout << "id: " << ms.id << " model: " << ms.ModelName << '\n';
        std::copy(ms.number.begin(), ms.number.end(),
                  std::ostream_iterator<int>(std::cout, " "));
        std::cout << '\n';
    }
}

6 Comments

Thank you for the reply. What I don't understand is ms.number.push_back(5); {1, "model_a", {2,3,4}}, {2, "model_b", {5,6,7,8}} If I wanted to add 99 to the "model a" which is ID = 1 here, and at the same time What if I want to delete 7 from "model b" which is ID = 2? How does it work?
Lyngmo I updated by adding the example I wanted to ask . Thank you so much.
I'm trying it right now. I didn't update the page when I wrote it
I've tried what you've written, thank you very much. But if I wanted to add a new model, there was a problem. demoListe.insert (3, "Model_C", { 31,32,33,34,35 }); I couldn't log in like this, do I have to use a different method?
godbolt.org/z/cQ9jpH ............ Thank you so much for everything. I'm going to learn and improve by studying the code you've written in detail.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.