how do I get sequence of 'false' in an array - c++

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";
}

Related

How do I iterate vector

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;
}

Why don't people initialize i globally?

So, it seems like 'i' is pretty much the universal counter in C++. It seems like in every for loop, people re-initialize 'i'. I have to ask, why don't they just initialize 'i' globally? 'i' would still have to be re-defined in each loop, so I don't see why there would be any confusion.
It just seems like this:
#include <iostream>
int i=0;
int main()
{
for (i=0;i<3;i++)
{
std::cout << i << "\n";
}
for (i=0;i<5;i++)
{
std::cout << "hello" << "\n";
}
return 0;
}
is easier to read, and faster to write than:
#include <iostream>
int main()
{
for (int i=0;i<3;i++)
{
std::cout << i << "\n";
}
for (int i=0;i<5;i++)
{
std::cout << "hello" << "\n";
}
return 0;
}
Excellent idea!
Here's a program that prints "hellohello" five times:
int i;
void print_twice(const std::string& s)
{
for (i = 0; i < 2; i++)
{
std::cout << s;
}
std::cout << std::endl;
}
int main()
{
for (i = 0; i < 5; i++)
{
print_twice("hello");
}
}
Or... does it? (Ominous organ music plays. The crows caw. Sirens in the distance.)

For loop prints an extra comma

cout<<"Set B : {";
for(i=0;i<b;i++)
{
cout<<setB[i];
cout<<",";
}
cout<<" }"<<endl;
The code above is not printing correctly. It should print Set B : {1,2,3} but it prints an extra comma ==> Set B : {1,2,3,}
Use
cout << "Set B : {";
for (i = 0; i < b; ++i) {
if (i > 0) cout << ",";
cout << setB[i];
}
cout << " }" << endl;
I changed your algorithm :
Before it meant : "Put the number and then put a comma"
Now it means : "If there is a number behind me put a comma, then put the number"
Before, you always printed a comma when you printed a number so you had an extra comma.
For each iteration of the for loop, the program is going to execute -everything- inside the for loop. So, your loop runs through and prints each number in your set and then a comma.
The problem is that even on your last run through the loop, it is going to print a comma, because it's part of the loop.
cout << "Set B : {";
for(i = 0; i < b; i++){
cout << setB[i];
if (i < (b-1))
cout << ",";
}
cout << " }" << endl;
This code will run the exact same, except the second to last time it runs through the loop, it will not print a comma. No need to get too fancy. :)
Personally I like this solution better. You first print out the first element and then a , [second element].
cout <<"Set B : {" << setB[0];
for(i = 1; i < b; i++)
{
cout << ",";
cout<<setB[i];
}
cout << " }" << endl;
Warning!: This will NOT work if the array is empty.
The loop code prints a pair of number and comma. Try using this one:
cout<<"Set B : {";
for(i=0;i<b;i++)
{
cout<<setB[i];
if(i < b-1) cout<<",";
}
cout<<"}"<<endl;
You're loop is executing the cout << "," 3 times. The following will give what you want:
#include <iostream>
using namespace std;
int main(){
int setB[] = {1,2,3};
cout<<"Set B : {";
for(int i=0;i<3;i++)
{
cout<<setB[i];
if ( i < 2 )
cout<<",";
}
cout<<" }"<<endl;
return 0;
}
The way I often deal with these loops where you want to put something like a space or a comma between a list of items is like this:
int main()
{
// initially the separator is empty
auto sep = "";
for(int i = 0; i < 5; ++i)
{
std::cout << sep << i;
sep = ", "; // make the separator a comma after first item
}
}
Output:
0, 1, 2, 3, 4
If you want to make it more speed efficient you can output the first item using an if() before entering the loop to output the rest of the items like this:
int main()
{
int n;
std::cin >> n;
int i = 0;
if(i < n) // check for no output
std::cout << i;
for(++i; i < n; ++i) // rest of the output (if any)
std::cout << ", " << i; // separate these
}
An other way, without extra branch:
std::cout << "Set B : {";
const char* sep = "";
for (const auto& e : setB) {
std::cout << sep << setB[i];
sep = ", ";
}
std::cout <<" }" << std::endl;
I really like to promote the use of a range library to write declarative code instead of nested for-if statements in an imperative style.
#include <range/v3/all.hpp>
#include <vector>
#include <iostream>
#include <string>
int main()
{
using namespace ranges;
std::vector<int> const vv = { 1,2,3 };
auto joined = vv | view::transform([](int x) {return std::to_string(x);})
| view::join(',');
std::cout << to_<std::string>(joined) << std::endl;
return 0;
}
If you can use STL, try the following:
#include <iterator>
#include <iostream>
int main() {
int setB[]{1,2,3};
std::cout << "Set B : { ";
for(auto i = std::begin(setB), e = std::end(setB); i != e;) {
std::cout << *i;
for(++i; i !=e; ++i) { std::cout << ", " << *i; }
}
std::cout << " }" << std::endl;
return 0;
}

How to print elements in a vector c++

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.

Changing original vector does not change it in a collection

Why do I get different output? How can I fix this? I want the trainingVector[0] to reference A.
vector<double> A(4,0);
vector<vector<double > > trainingVector;
A[0]=1;
trainingVector.push_back(A);
A[0]=2;
cout << A[0] << endl ;
cout << trainingVector[0][0] << endl ;
You cannot store references in STD containers, so what you ask for is impossible. If you want trainingVector to store a pointer to A, that's entirely doable:
vector<double> A(4,0);
vector<vector<double>*> trainingVector;
A[0] = 1;
trainingVector.push_back(&A);
A[0] = 2;
// notice that you have to dereference trainingVector[0] to get to A
cout << (*trainingVector[0])[0] << endl; // prints 2
You could store a pointer to A instead:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> A(1);
A[0] = 1;
std::vector<std::vector<int>*> trainingVector;
trainingVector.push_back(&A);
A[0] = 2;
std::cout << A[0] << std::endl;
std::cout << (*trainingVector[0])[0] << std::endl;
return 0;
}
Alternatively, if you really want the syntax as specified in the question, you could do something like this:
#include <iostream>
#include <vector>
template <typename T>
class VecRef
{
private:
std::vector<T> *m_v;
public:
VecRef(std::vector<T>& v)
: m_v(&v)
{}
T& operator[](int i)
{
return (*m_v)[i];
}
};
int main()
{
std::vector<int> A(1);
A[0] = 1;
std::vector<VecRef<int>> trainingVector;
trainingVector.push_back(A);
A[0] = 2;
std::cout << A[0] << std::endl;
std::cout << trainingVector[0][0] << std::endl;
return 0;
}