I need to be able to call a function that is looking for an iterator of a complex data structure (pseudo-code) vector::deque::vector(uint8_t)::iterator. I need to be able to call it with a deque::vector(uint8_t); I can not figure out how to "iterate" it.
In the code segment below, I'm trying to call the MyFunkyFunc function with the someMoreBytes deque structure.
#include <cstdlib>
#include <vector>
#include <deque>
#include "stdint.h"
using namespace std;
void MyFunkyFunc(std::vector<std::deque<std::vector<uint8_t>>>::iterator itsIt)
{
}
int
main(int argc, char** argv)
{
std::vector<std::deque<std::vector < uint8_t>>> bunchaBytes;
std::deque<std::vector<uint8_t>> someMoreBytes;
//... Put at least one element in bunchaBytes
MyFunkyFunc(bunchaBytes.begin());
MyFunkyFunc(someMoreBytes); // Problem is here
return 0;
}
This code stub is a close as I can get to the original; I am unable to make any modifications to the MyFunkyFunc function, as it is in a library I have to link with. Many thanks in advance
If we assume that MyFunkyFunc was implemented properly as a template accepting an iterator parameter:
template <typename I>
void MyFunkyFunc (I itsIt) {
//...
}
Then, you can just pass the address of someMoreBytes, since an iterator to a vector behaves the same as the address of an element of a vector.
MyFunkyFunc(&someMoreBytes);
Otherwise, you will need to redefine someMoreBytes to be a single element vector, and pass in the begin(), just as you did with bunchaBytes.
Related
I have an issue where I need to keep a map of, e.g. vectors, of items, each vector has a corresponding indicator valid for all items for a given key in the map
I guess it could be map of pairs (map<string,pair<vector,bool>>) but that would e very confusing...
So
I wanted to ask you about code like that:
Would this be considered a correct implementation of the problem, is there any potential issue with this? When I add a new key with myMap["KEY"] nothing can ever break because it autoinitializes my struct? Can anything go wrong here?
#include <iostream>
#include <vector>
#include <map>
using namespace std;
struct ListOfItemsWithIndicator
{
bool _indicator;
vector<int> _items;
ListOfItemsWithIndicator(): _indicator(false) {}
};
int main() {
std::map<std::string,ListOfItemsWithIndicator> myMap;
myMap["ONE"]._items.push_back(1);
std::cout << myMap["ONE"]._items[0];
return 0;
}
I'm new to C++ and trying to understand a simple example of inserting a list of integers into a map.
#include <iostream>
#include <map>
#include <list>
using namespace std;
map<string, list<int>> m;
void insert(list<int>& list_to_insert)
{
m.insert({"ABC", list_to_insert});
}
void setup()
{
std::list<int> local_list = { 7, 5, 16, 8 };
insert(local_list);
}
int main()
{
setup();
cout << m["ABC"].size(); // PRINTS 4
}
As far as my understanding, local_list is a variable only known to the setup function. When I pass in a reference to the insert function, I expect it to work. However, at the end of setup, I expect local_list to be removed from the stack frame. However, in my main function, when I call size(), I see that it has in fact persisted throughout. I am not sure I understand how local_list gets persisted at the end of setup(). What is exactly happening here?
map<string, list<int>> m;
contains lists, not references to lists.
So your
m.insert({"ABC", list_to_insert});
will create a copy of the passed list.
PS: why-is-using-namespace-std-considered-bad-practice
hey guys i have created a struct node. one of its fields is a vector (path) where i want to store characters.however when i try to push_back a character the compiler says "error: ‘path’ was not declared in this scope"
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <list>
#include <climits>
using namespace std;
struct node {
int weight;
bool pizza; // true an tin exo
vector <char> path;
int tetmimeni, tetagmeni; // i, j gia na vro geitones
} ;
node a;
int main(){
a.tetmimeni=0; // create start node
a.tetagmeni=0;
a.weight=0;
a.pizza=true;
a.path= path.push_back('S');
Replace a.path= path.push_back('S'); with just a.path.push_back('S');
The original code was trying to assign the return type of push_back to a.path which is invalid.
Instead you simply want to invoke the push_back method of the std::vector member of your struct.
In your code , node is a structure. Path is one element of struct.
Anytime you need to access element of struct , you have to use the name of struct along with it.
e.g. a.pizza or a.weight when 'a' is of the type node.
Similarly you need to access a.path when you want to access vector path. It doesn't matter even if you need to call the functions of vector.
You should go through struct/class
I have many calls to a function that takes just one argument and I don't want update those calls. But I want to call that function from some other special place but in that case it should additionally fill a vector that I will pass with some data.
I know I can create a default argument with NULL pointer to a std::vector container and then, if it is null, skip doing any extra actions and if it is a valid pointer - gather data to vector. However I wanted to try using boost::optional.
Please see the code below. It compiles and works, but Is this approach fine or I shouldn't do that and better use raw pointer?
#include <boost/optional.hpp>
#include <boost/none_t.hpp>
#include <vector>
//header file declaration
int doAction(
int value,
char *msg = NULL,
boost::optional<std::vector<int>&> optionalNumberVec = boost::none);
//main.cpp
int doAction(int value, char* msg, boost::optional<std::vector<int>&> optionalNumberVec)
{
//do main actions here
//...
//...
//end of main action
//get additional information to table
if (optionalNumberVec)
{
optionalNumberVec.get().push_back(5);
optionalNumberVec.get().push_back(3);
}
return 1;
}
int main()
{
std::vector<int> numVec;
boost::optional<std::vector<int>&> optionalNumberVec(numVec);
doAction(2);
doAction(2, NULL, optionalNumberVec);
return 0;
}
Using boost or not is a simple decision based on your preferences (or your boss's preferences).
Once you get used to C++ you will notice that it doesn't really matter which one you use, as long you know how to use them.
What is the most correct and efficient way to std::move elements from a vector of a certain type (T1) into a vector of an std::pair of that same type (T1) and another type (T2)?
In other words, how should I write MoveItems()?
#include <iostream> // For std::string
#include <string> // For std::string
#include <vector> // For std::vector
#include <utility> // For std::pair
using std::vector;
using std::string;
using std::pair;
vector<string> DownloadedItems;
vector<pair<string,bool>> ActiveItems;
vector<string> Download()
{
vector<string> Items {"These","Words","Are","Usually","Downloaded"};
return Items;
}
void MoveItems()
{
for ( size_t i = 0; i < DownloadedItems.size(); ++i )
ActiveItems.push_back( std::pair<string,bool>(DownloadedItems.at(i),true) );
}
int main()
{
DownloadedItems = Download();
MoveItems();
return 0;
}
Thank you for your time and help, I truly appreciate it!
void MoveItems()
{
ActiveItems.reserve(DownloadedItems.size());
for (auto& str : DownloadedItems)
ActiveItems.emplace_back(std::move(str), true);
}
N.B.: For strings as small as the ones in your example, moving may have the same cost as copying due to SSO, or perhaps even slightly more expensive if the implementation decides to empty out the source anyway.
Some things you can do:
At the start of MoveItems(), call ActiveItems.reserve(DownloadedItems.size());. This prevents your array from resizing while you push things into it.
Instead of calling push_back call emplace_back. Here is an explanation of the advantages of doing so.
Worth noting, in this example, you can stop the copy into a new data structure by just constructing the std::pair from the start, and not copying data.