struct

How to have a set of structs in C++

早过忘川 提交于 2020-01-22 05:48:06
问题 I have a struct which has a unique key. I want to insert instances of these structs into a set. I know that to do this the < operator has to be overloaded so that set can make a comparison in order to do the insertion. The following does not work: #include <iostream> #include <set> using namespace std; struct foo { int key; }; bool operator<(const foo& lhs, const foo& rhs) { return lhs.key < rhs.key; } set<foo> bar; int main() { foo *test = new foo; test->key = 0; bar.insert(test); } 回答1:

Initalizing the nested anonymous structures

左心房为你撑大大i 提交于 2020-01-22 03:58:05
问题 I am having a json as { "fields": ["time","id","status","customerId","additionalDetail"], "pageInfo": {"start": 0, "rows": 1000} } I wanted to Marshal my structure to above json and create the structure as below - type RBody struct { Fields []string `json:"fields"` PageInfo struct { Start int `json:"start"` Rows int `json:"start"` } `json:"pageInfo"` } I am having trouble in initializing the above structure. I am not sure how to initialize the anonymous struct in below fashion : bd := RBody {

Is it possible to have a struct with multiple JSON tags?

我只是一个虾纸丫 提交于 2020-01-21 11:52:12
问题 I post a request to a server and get a reply in JSON format. I'm able to unmarshal it to a struct. Then I need to create a new JSON file with the same data but different JSON tags. Example: In the following code, I get {"name":"Sam","age":20} from a server and unmarshal it to the struct Foo : type Foo struct { Name string `json:"name"` Age int `json:"age"` } Then I need to change the tag name to employee_name and omit age : type Bar struct { Name string `json:"employee_name"` Age int `json:"-

Can we have an anonymous struct as template argument?

我与影子孤独终老i 提交于 2020-01-21 06:29:38
问题 The title is pretty self-explanatory, but here's a simplified example: #include <cstdio> template <typename T> struct MyTemplate { T member; void printMemberSize() { printf("%i\n", sizeof(T)); } }; int main() { MyTemplate<struct { int a; int b; }> t; // <-- compiler doesn't like this t.printMemberSize(); return 0; } The compiler complains when I try to use an anonymous struct as a template argument. What's the best way to achieve something like this without having to have a separate, named

How can I use an unordered_set with a custom struct?

无人久伴 提交于 2020-01-21 04:48:25
问题 I want to use an unordered_set with a custom struct . In my case, the custom struct represents a 2D point in an euclidean plane. I know that one should define a hash function and comparator operator and I have done so as you can see in my code below: struct Point { int X; int Y; Point() : X(0), Y(0) {}; Point(const int& x, const int& y) : X(x), Y(y) {}; Point(const IPoint& other){ X = other.X; Y = other.Y; }; Point& operator=(const Point& other) { X = other.X; Y = other.Y; return *this; };

__unsafe_unretained NSString struct var

一笑奈何 提交于 2020-01-21 03:43:04
问题 I am trying to create a structure that has several different variable of different types in it. several of the types are of NSString, but trying to do this was causing an error ARC forbids Objective-C objects in structs or unions so having read about the error I see its sensible to add __unsafe_unretained before the NSString declaration, However I have no idea what the ramifications of this will be, I have had a quick read around and found this detailed post about the differences of __strong

Why can't I mutate a variable initially set to a certain parameter when the func was called?

依然范特西╮ 提交于 2020-01-20 08:37:08
问题 GOAL: I'm trying to make a general struct that can take an array of Ints and go through and set a timer for each one (and show a screen) in succession. Problem: I get Escaping closure captures mutating 'self' parameter error as shown in the code. import SwiftUI struct ContentView: View { @State private var timeLeft = 10 @State private var timers = Timers(timersIWant: [6, 8, 14]) // var timersIWantToShow: [Int] = [6, 8, 14] var body: some View { Button(action: {self.timers.startTimer(with:

How can I make a container with copy-on-write semantics? (Swift)

孤者浪人 提交于 2020-01-20 04:20:05
问题 I have a very large structure, which I want to ensure is not copied needlessly. How can I make a copy-on-write container for it? 回答1: A copy-on-write is usually a struct wrapper over some backing object. public final class MutableHeapStore<T>: NonObjectiveCBase { public typealias Storage = T public private(set) var storage: Storage public init(storage: Storage) { self.storage = storage } } public struct COW<T> { public typealias Storage = MutableHeapStore<T> public typealias Value = T public

Can __attribute__((packed)) affect the performance of a program?

徘徊边缘 提交于 2020-01-20 03:55:11
问题 I have a structure called log that has 13 chars in it. after doing a sizeof(log) I see that the size is not 13 but 16. I can use the __attribute__((packed)) to get it to the actual size of 13 but I wonder if this will affect the performance of the program. It is a structure that is used quite frequently. I would like to be able to read the size of the structure (13 not 16). I could use a macro, but if this structure is ever changed ie fields added or removed, I would like the new size to be

What is the difference between “struct” and lack of “struct” word before member of a struct

ぃ、小莉子 提交于 2020-01-19 18:06:32
问题 I have to create simple List implementation. They guy who wants that put struct before member next of class Node . Why is there a struct word, what would be the difference without it? struct Node{ int value; struct Node *next;//what is this struct for? }; struct List{ struct Node *first, *last; }; 回答1: In your example, there is no need to use the struct keyword before the next declaration. It is usually considered a throw-back from C, where it is required. In C++, this would suffice: struct