-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed unnecessary stuff & experimenting with idea of naming vector …
…elements
- Loading branch information
1 parent
680cc5c
commit 1fd466e
Showing
2 changed files
with
48 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <vector> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <iostream> | ||
|
||
class NamedVector { | ||
private: | ||
std::vector<double> values; | ||
std::unordered_map<std::string, size_t> name_to_index; | ||
|
||
public: | ||
// Add an element with a name | ||
void add_element(const std::string& name, double value) { | ||
values.push_back(value); | ||
name_to_index[name] = values.size() - 1; | ||
} | ||
|
||
// Get element by name | ||
double get_element(const std::string& name) const { | ||
auto it = name_to_index.find(name); | ||
if (it != name_to_index.end()) { | ||
return values[it->second]; | ||
} | ||
throw std::runtime_error("Name not found"); | ||
} | ||
|
||
// Display all elements and their names | ||
void display() const { | ||
for (const auto& pair : name_to_index) { | ||
std::cout << pair.first << ": " << values[pair.second] << std::endl; | ||
} | ||
} | ||
}; | ||
|
||
int main() { | ||
NamedVector nv; | ||
nv.add_element("a", 1.0); | ||
nv.add_element("b", 2.0); | ||
nv.add_element("c", 3.0); | ||
|
||
std::cout << "Element 'b': " << nv.get_element("b") << std::endl; | ||
|
||
std::cout << "All elements:" << std::endl; | ||
nv.display(); | ||
|
||
return 0; | ||
} | ||
|
This file was deleted.
Oops, something went wrong.