1

In java, something like this is possible (shown as an example)

new Thread(new Runnable() {
    /*.....code.......*/
});

We provided an object constructed and defined in-place(the Runnable()), in the argument for the object constructor(Thread class). Can we do something like that in c++?

what I want to do in my code is

void put(int key, int value) {
    store.insert(store.begin(), vector<int> {{key, value}});
}
/**
type(store) = vector<vector<int>>
*/

and avoid something like

void put(int key, int value) {
    vector<int> temp{key, value};
    store.insert(store.begin(), temp);
}
Vishwad
  • 251
  • 3
  • 18
  • 2
    C++ is not Java. C++ uses templates and closures, to accomplish the same end result. `std::thread`'s constructor works with any closure or a callable object. The End. – Sam Varshavchik May 31 '21 at 01:03
  • I am sorry but that did not answer my question. Let me edit the question details. – Vishwad May 31 '21 at 01:04
  • 1
    While you're editing, you should get rid of `vector` altogether. Attempting to create such an object, in C++, will always end in tears. – Sam Varshavchik May 31 '21 at 01:05
  • could you elaborate? – Vishwad May 31 '21 at 01:05
  • @Vishwad arrays are not assignable, e.g. `int x[2] = {}, y[2] = {}; x = y` will not compile. `std::vector` will use `=` on its contained elements often and this will cause problems. – alter_igel May 31 '21 at 01:06
  • To be pedantic, `std::vector` requires a movable or a copyable object. A plain array is neither, in C++. So, you are going to already fail at that step, even before getting to the question you're asking, about "in place" construction. Focus on fixing the immediate issue first, and then a discussion about "in place" construction can follow. – Sam Varshavchik May 31 '21 at 01:07
  • Thank you very much for that knowledge. could vector> be a decent replacement for vector? – Vishwad May 31 '21 at 01:07
  • It is a perfectly fine replacement. In fact, once made, there's an excellent chance that your code will compile as shown, and that was the `Y` of your `XY` problem. – Sam Varshavchik May 31 '21 at 01:08
  • 1
    `vector` would also work fine if you prefer direct storage and a fixed size. – alter_igel May 31 '21 at 01:08
  • Did you run into a problem using the C++ code from your question? Why not "just do it"? – JaMiT May 31 '21 at 01:21
  • You should put together a [mre]. You might find you solve the problem yourself in the process. – Paul Sanders May 31 '21 at 01:24
  • 1
    The best way to answer "will this work" type questions is to try it and see. – Dawood ibn Kareem May 31 '21 at 01:36
  • 1
    @Vishwad *I am sorry but that did not answer my question.* -- First, answers go in the answer section, not in the comment section. The comment section is where comments go. But to the broader point -- if you're using Java as a model in writing C++ code, you will get into trouble. As the comment stated, Java is not C++ and C++ is not Java. What you will wind up having if continuing to use Java as a model in writing C++ code is either 1) Buggy C++ programs, 2) inefficient C++ programs, 3) Programs with memory leaks or 4) A program that looks strange to a C++ programmer. – PaulMcKenzie May 31 '21 at 03:49

3 Answers3

1

First of all, thank you everyone for your help and suggestions in the comments. Something clicked in my head and I think I managed to find the answer. This worked

void put(int key, int value) {
    store.insert(store.begin(), vector<int>({key, value}));
}

Explanation: vector<int>() is the constructor where I provided the list of values {key, value} to construct a vector.

Apart from the knowledge shared by everyone. You might want to look at explanation for working about why this code works.

Vishwad
  • 251
  • 3
  • 18
  • Next lesson: [emplace()](https://en.cppreference.com/w/cpp/container/vector/emplace) so that you provide the construction arguments instead of providing an object. Let the vector construct the object in-place rather than having the vector move or copy the temporary object your code constructed. *Follow-up lesson: don't be blinded by your knowledge of Java.* – JaMiT May 31 '21 at 01:41
  • This works too: `store.insert(store.begin(), {key, value});` – kritzikratzi May 31 '21 at 03:47
0

To answer your first question: Your Java code with the runnable creates an anonymous class with a custom constructor, something like that doesn't exist in C++ (to my knowledge).

However, C++ does just fine without it. A thread can be supplied with a lambda, like so:

thread t([](){
    std::cout << "hi from thread" << std::endl;
});

For your other question: vectors can be constructed from initializer lists, meaning such things are valid:

vector<int> list_1(4,5);    // take care!! list is 5,5,5,5!
vector<int> list_2{4,5};    // list_2 is 4,5
vector<int> list_3 = {4,5}; // list_3 is 4,5
list_1 = {1,2,3};           // list_1 is now 1,2,3

// all the above are using so called initializer lists

vector<vector<int>> store;
store.insert(store.begin(), {1,3});  // store is {{1,3}}
store.insert(store.begin(), {1,3,3});// store is {{1,3},{1,3,3}}
store = {{4},{7,8},{1,2,3}};         // store is {{4},{7,8},{1,2,3}}

struct KV{int key; int value;};
vector<KV> store2{{1,5},{7,8}};      // store has two KVs,{1,5} and {7,8}
store2.insert(store2.begin(), {3,3});// ... and now also {3,3}

// note:creating a KV by doing KV x{4,5} is called
// uniform initialization. 
// it's the same syntax as list initializers, but they are different 
// things! 
kritzikratzi
  • 19,662
  • 1
  • 29
  • 40
0

Java can define a class(and instantiate it) without its name that called anonymous class. C++ cannot do that, the example C++ code in your question is not same thing which comparing to your java code. You must define a struct or class explicitly in C++

jean
  • 2,825
  • 2
  • 35
  • 72