I have such a code
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void removeFirstFromVec(vector<int> & vecLink) {
// remore first element from some vector
vecLink.erase(vecLink.begin() + 0);
}
int main()
{
vector<int> myVec;
myVec.push_back(1);
myVec.push_back(2);
myVec.push_back(3);
cout << "Before removal\n";
for (auto & i : myVec) {
cout << myVec[i-1] << endl;
}
removeFirstFromVec(myVec);
cout << "After removal\n";
for (auto & i : myVec) { // starts with 2
cout << myVec[i-1] << endl;
}
return 0;
}
But on the place where I put a comment it starts with 2 instead of 0 and it causes an error. What I've done wrong or is there a way to use something brief like auto & i : myVec instead of for (int i = 0; i < ...size(); i++) without such an error
In the range-based for loop
for (auto & i : myVec) {
cout << myVec[i-1] << endl;
}
i is the element of the vector, not the index. It should be
for (auto & i : myVec) {
cout << i << endl;
}
Iteration returns the items themselves, not indices.
vector<int> vec { 5, 3, 2 };
for (int i: vec) {
cout << i << endl;
}
Will output this:
5
3
2
If you do the following:
Use templated-function to accept any type for the vector.
Use direct initialization method rather than push_back() to avoid huge number of lines.
Braces for single syntax is optional. Avoid it to reduce confusion during coding complex programs.
Then you can write the same code in a better manner (don't forget the comments):
#include <iostream>
#include <vector>
using namespace std;
// defining a templated-function
template<class T>
void removeFirstFromVec(vector<T>& vecLink) {
vecLink.erase(vecLink.begin());
}
int main(void) {
// initializing with direct-initialization method
vector<int> myVec {1, 2, 3};
cout << "Before removal\n";
// no braces
for (auto& i : myVec)
// 'i' isn't an index here, it's an element
cout << i << endl;
removeFirstFromVec(myVec);
cout << "After removal\n";
// no braces
for (auto& i : myVec)
cout << i << endl;
return 0;
}
Related
I tried on my own. But I was unable to do it. So, please help.
unordered_map<string, pair<string , vector<int>>> umap;
Or more precisely, how We can make pair of one string and one vector that can be used in map.
Well you can use insert function and insert them as a pair (Or precisely nested pairs).
For example Checkout this program :
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, pair<string , vector<int>>> umap;
//Insert like this
umap.insert(make_pair("first", make_pair("data1",vector<int>(3, 0))));
umap.insert(make_pair("second", make_pair("data2",vector<int>(3, 1))));
//or like this
string s = "new", t= "method";
vector <int> v = {1, 2, 3};
umap.insert(make_pair(s, make_pair(t, v)));
//This will print all elements of umap
for(auto p : umap)
{
cout << p.first << " -> " << p.second.first << " , VECTOR : " ;
for(auto x : p.second.second)
cout << x << ',';
cout << endl;
}
cout << endl;
//Let's see how to change vector already inside map
auto itr = umap.begin();
cout << "BEFORE : ";
for(auto x : (itr->second).second)
{
cout << x << ',';
}
cout << endl;
//We will push 42 here
(itr->second).second.push_back(42);
cout << "AFTER : ";
for(auto x : (itr->second).second)
{
cout << x << ',';
}
cout << endl;
}
Output Is :
new -> method , VECTOR : 1,2,3,
second -> data2 , VECTOR : 1,1,1,
first -> data1 , VECTOR : 0,0,0,
BEFORE : 1,2,3,
AFTER : 1,2,3,42,
I hope this helps.
This depends a lot on the complexity of what you are creating. For example, if you have some constants in your vector, you could make them in place:
umap.emplace("s", std::make_pair("s2", std::vector<int>{1, 2, 3, 4}));
It is more likely however that you will be making the internals in some complex way. In which case you could more easily do it as separate constructions.
std::vector<int> values;
values.push_back(1);
auto my_value = std::make_pair("s2", std::move(values));
umap.emplace("s2", std::move(my_value));
Using move to move the data around ensures minimal copying.
Finally, to iterate the items, it is normal to use range-based for loops:
for (const auto& [key, value]: umap) {
std::cout << key << ": ";
const auto& [name, values] = value;
std::cout << name << " {";
for (auto val : values) {
std::cout << val << " ";
}
std::cout << "}\n";
}
Here you can check out a live example.
Why does item.someVector.push_back(1); seem to work, but not item.getSomeVector().push_back(1); ?
Here is my test case:
#include <iostream>
#include <vector>
using namespace std;
class Item {
public:
vector<int> someVector = vector<int>();
vector<int> getSomeVector()
{
return someVector;
}
};
int main()
{
Item item = Item();
item.getSomeVector().push_back(1);
cout << item.getSomeVector().size() << endl;
cout << item.someVector.size() << endl;
item.someVector.push_back(1);
cout << item.getSomeVector().size() << endl;
cout << item.someVector.size() << endl;
}
// output:
// 0
// 0
// 1
// 1
Because getSomeVector returns a copy of someVector. So you're updating a temporary object which is then immediately destroyed. Try changing it to vector<int>& getSomeVector()
I am the beginner of C++, and any help will be very appreciated.
here is the code i can run successfully:
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
main(){
bool findIn=false;
RowVectorXd A(10);
A<<false,true,false,true,true,false,false,false,true,true;
std::cout << A << std::endl;
for (int i=0;i<A.size();i++){
if(A(i)==findIn){
std::cout << i << std::endl;
}
}
system("pause");
}
the result is {0,2,5,6,7}, and I want to design a function, the code is as follows:
int seq(bool findIn, VectorXd &resdX){
VectorXd A;
for(int i=0;i<resdX.size();i++){
if(resdX(i)==findIn){
A =A+i;
}
}
return(A);
}
I want this function to return result like that {0,2,5,6,7}.But I don`t know how to set up a array to save the result or is there a function just like 'which' in R software to produce sequence above.
Sounds like you want a vector of integers:
#include <vector>
std::vector<int> seq(bool findIn, VectorXd &resdX)
{
std::vector<int> v;
for(int i=0;i<resdX.size();i++) {
if (resdX(i) == findIn) {
v.push_back(i);
}
}
return v;
}
You can then print its contents by iterating through it:
std::vector<int> result = seq(false, A);
for (int i : result) std::cout << i << '\n';
I did not understand what are you looking for.
If you want just to print a sequence of integers indexing findIn values you might code:
void seq(bool findIn, VectorXd &resdX) { // not int
std::cout << "{ ";
for(int i=0;i<resdX.size();i++) // go inside the array
if(resdX(i)==findIn) // if you find that resdX(i) value equals findIn value
std::cout << i << " "; // print i index
std::cout << "}" << std::endl; // at the end prints a new line
}
EDIT1:
Try to adapt the following snippet:
#include <list>
...
std::list<int> seq(bool findIn, VectorXd& resdX) {
std::list<int> l;
for(int i=0; i<resdX.size(); i++) {
if (resdX(i) == findIn) {
l.push_back(i);
}
}
return l;
}
void print_seq(std::list<int> list_) {
std::cout << "{ ";
std::list<int>::iterator it = list_.begin();
for (; it != list_.end(); ++it) {
std::cout << i << " ";
}
std::cout << " }\n";
}
boost range transform requires const & for ranges in arguments.
#include <iostream>
#include <vector>
#include <boost/range/algorithm.hpp>
int main(int argc, char *argv[])
{
using namespace std;
vector<vector<int>> rt0(10,vector<int>(15,2));
vector<vector<int>> irt(10,vector<int>(15,5));
for(auto & i:rt0) {
for(auto& j:i) cout << j << " ";
cout << "\n";
}
cout << "\n";
for(auto & i:irt) {
for(auto& j:i) cout << j << " ";
cout << "\n";
}
boost::transform(rt0,irt,rt0.begin(),
[] (const vector<int> &t0,const vector<int> &it) {
auto tt = t0;
boost::transform(t0,it,tt.begin(), plus<int>());
return tt;
}
);
cout << "\n";
cout << "\n";
for(auto & i:rt0) {
for(auto& j:i) cout << j << " ";
cout << "\n";
}
return 0;
}
compile and run with
g++ -std=c++11 main.cc; ./a.out
if boost::transform's BinaryOperation took & instead of const & for SinglePassRange1 then i wouldn't have needed to create a copy (auto tt = t0) and just use to instead of tt. is there a way to avoid creating this copying (while still using ranges)?
link to boost range transform: http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/reference/algorithms/mutating/transform.html
I need to use for_each either with a tuple or the new boost implementation that takes two arguments.
conceptually transform should not modify. it comes from functional programming where there is no in-place modification.
I am having trouble with my void print function to print out this vector. I'm not quite sure what it is talking about with "std::allocator. I get these errors:
st1.cpp: In function ‘void Print(std::vector<int, std::allocator<int> >)’:
st1.cpp:51: error: declaration of ‘std::vector<int, std::allocator<int> > v’ shadows a parameter
Here is the file:
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
void Initialize();
void Print();
int main()
{
stack<string> s1, s2;
s1.push("b");
s2.push("a");
if (s1.top() == s2.top())
{
cout << "s1 == s2" << endl;
}
else if (s1.top() < s2.top())
{
cout << "s1 < s2" << endl;
}
else if (s2.top() < s1.top())
{
cout << "s2 < s1" << endl;
}
else
{
return 0;
}
vector<int> v;
Initialize();
Print();
}
void Initialize(vector<int> v)
{
int input;
cout << "Enter your numbers to be evaluated: " << endl;
while(input != -1){
cin >> input;
v.push_back(input);
//write_vector(v);
}
}
void Print (vector<int> v){
vector<int> v;
for (int i=0; i<v.size();i++){
cout << v[i] << endl;
}
}
I just want to print v out to the screen. Any help?
Your function declaration and definition are not consistent, you want to generate vector from Initialize, you can do:
void Initialize(vector<int>& v);
To print vector:
void Print(const vector<int>& v);
Now you call:
vector<int> v;
Initialize(v);
Print(v);
Don't forget to change function definition of Initialize, Print to match the new signature I provided above.
Also you are redefining a local variable v which shadows function parameter, you just need to comment out that line, also pass vector by const ref:
void Print (const vector<int>& v){
//vector<int> v;
for (int i=0; i<v.size();i++){
cout << v[i] << endl;
}
}
You have to pass by const reference and remove the extraneous vector.
void Print(const std::vector<int>& v){
for(unsigned i = 0; i< v.size(); ++i) {
std::cout << v[i] << std::endl;
}
}
Another way to print it out would be to use iterators like so:
void Print(const std::vector<int>& v) {
std::vector<int>::iterator it;
for(it = v.begin(); it != v.end(); ++it) {
std::cout << (*it) << '\n';
}
}
Or in C++11 you can do it like so:
void Print(const std::vector<int>& v) {
for(auto& i : v)
std::cout << i << '\n';
}
I don't think your Initialize() function works like you expect it to. It seems to just make a copy and then discards it, not modifying any values of the existing vector.
Leveraging c++ std's 11 onwards, this could be acheived in one line as shown below.
std::for_each(v.begin(),v.end(),[](const int &i) { std::cout << i; });
This code runs a for loop using std::vector iterators from begin() to end(), serially feeding the lambda/[] function the required argument to be printed.