Ask Your Question
-1

How to push_back to a vector

asked Oct 21 '13

dogucan159 gravatar image

This is my vector.

vector (Vec4i) lines; I have tried this but It is not working.

lines.push_back((0,0,10,10));

How can i do this job correctly?

Preview: (hide)

Comments

1

-1: "It is not working" is not a helpful error description.

SR gravatar imageSR (Oct 21 '13)edit

2 answers

Sort by » oldest newest most voted
4

answered Oct 21 '13

Guanta gravatar image
std::vector<cv::Vec4i> lines // I guess this is what you have
lines.push_back(cv::Vec4i(0,0,10,10)); // in this way you can add another Vec4i to your lines-vector
Preview: (hide)

Comments

I have tried your recommend again and again but problem is go on.

dogucan159 gravatar imagedogucan159 (Oct 21 '13)edit
1

which error message do you get?

Guanta gravatar imageGuanta (Oct 22 '13)edit

_CrtIsValidHeapPointer(pUsrData) expression in error message

dogucan159 gravatar imagedogucan159 (Oct 22 '13)edit
1

not very informative - please compile in debug mode and use a debugger (e.g. gdb) to find the line of code which fails

Guanta gravatar imageGuanta (Oct 22 '13)edit

if(xaxis!=(it2)[0]) { line(skeleton,Point((it2)[0],yaxis),Point(xaxis,yaxis),color); writeToFile((it2)[0],yaxis,xaxis,yaxis,i); lines.push_back(Vec4i((it2)[0],yaxis,xaxis,yaxis)); // LINE OF CODE WHICH FAILS ++i; } //...

dogucan159 gravatar imagedogucan159 (Oct 22 '13)edit
1

Please show some more lines (maybe the complete function) and use sth like http://pastebin.com - this comment function is very bad to read code.

Guanta gravatar imageGuanta (Oct 22 '13)edit

http://pastebin.com/3H9Hb19F This is also .cpp file of .h file

dogucan159 gravatar imagedogucan159 (Oct 22 '13)edit
1

answered Oct 22 '13

stereomatching gravatar image

updated Oct 22 '13

In c++11, we have a better api--emplace_back

std::vector<cv::Vec4i> lines;
lines.emplace_back(0, 0, 10, 10);

clang++ and g++ already support emplace_back(remember to add the flag c++11 or c++0x) I don't know about the situation of visual c++

Remember to include your header file and make sure you link to the correct library

#include<vector>

#include <opencv2/core/core.hpp>

int main()
{
  std::vector<cv::Vec4i> lines;
  lines.emplace_back(0, 0, 10, 10);
}

if your compiler don't support emplace_back yet use push_back suggested by Guanta

Besides, please take a look at c++ primer 5 edition(ch1~ch16) This could help you write robust, efficient and elegant codes by c++

Preview: (hide)

Question Tools

Stats

Asked: Oct 21 '13

Seen: 4,913 times

Last updated: Oct 21 '13