Combining two lists in C++ - c++

If I had two lists A and B, how can I add all the elements of B to the beginning of list A, without "emptying" the B list. I basically want to just transfer copies of B list into the beginning of A list. I'm thinking about using insert and want to double check on the syntax.
I know if I was adding it to the end it would be:
A.insert(A.end(), B.begin(), B.end());
so to insert it at the beginning would it be:
A.insert(A.begin(), B.begin(), B.end());
??

That can be achieved with one line, using the fact a list can be iterated both ways.
copy(A.rbegin(), A.rend(), front_inserter(B));
Complete example (C++11 for list constructors and printing code, but answer is C++03 valid):
#include <list>
#include <iterator>
#include <algorithm>
#include <iostream>
int main() {
// Create lists
std::list<char> A = {'a','b'};
std::list<char> B = {'c','d'};
// Insert A at the beginning of B
copy(A.rbegin(), A.rend(), front_inserter(B));
// Print result
for(auto c : B)
std::cout << c;
std::cout << "\n";
return 0;
}

Make a temporary copy of the list you want to append without emptying, then splice the temporary list into the original. Here is and example.
#include <iostream>
#include <list>
#include <vector>
int main ()
{
std::list<int> mylist;
std::list<int>::iterator it;
std::list<int> otherList;
std::list<int> combinedList;
// set some initial values:
for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5
for (int i=6; i<=10; i++) otherList.push_back(i); // 6 7 8 9 10
std::list<int> temp = otherList;
combinedList = mylist;
it = combinedList.begin();
combinedList.splice(it, temp);
std::cout << "mylist contains:";
for (it=combinedList.begin(); it!=combinedList.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout<<"orignal appended list:"<<std::endl;
for(it=otherList.begin(); it!=otherList.end(); ++it)
std::cout<<' '<<*it;
std::cout<<'\n';
return 0;
}
http://ideone.com/e.js/Ol6Wk1

Related

How to get frequency of std:vectors in C++?

I have 5 vectors. I want to check how many times these vectors exist. I used the following code to compare if 2 vectors are equal, but now I have more than 2 vectors. I want to compare all these 5 vectors together and count how many times each vector exists.
How can I do it?
The output should be:
(0,0,1,2,3,0,0,0) = 2 time(s)
(0,0,1,2,3,4,0,0) = 1 time(s)
(0,0,2,4,3,0,0,0) = 1 time(s)
(0,0,6,2,3,5,6,0) = 1 time(s)
Here is my code:
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
void checkVec(vector<int> v){
vector<int> v0;
if(v0 == v){
cout << "Equal\n";
}
else{
cout << "Not Equal\n";
}
}
int main(){
vector<int> v1={0,0,1,2,3,0,0,0};
vector<int> v2={0,0,1,2,3,4,0,0};
vector<int> v3={0,0,2,4,3,0,0,0};
vector<int> v4={0,0,1,2,3,0,0,0};
vector<int> v5={0,0,6,2,3,5,6,0};
checkVec(v1);
return 0;
}
You can use std::map counting the number of occurences of each vector:
#include <map>
#include <vector>
#include <iostream>
using vec = std::vector<int>;
int main(){
vec v1={0,0,1,2,3,0,0,0};
vec v2={0,0,1,2,3,4,0,0};
vec v3={0,0,2,4,3,0,0,0};
vec v4={0,0,1,2,3,0,0,0};
vec v5={0,0,6,2,3,5,6,0};
std::map<vec,std::size_t> counter;
// Initializer list creates copies by default
// But you should not create vX variables anyway.
for(const auto& v: {v1,v2,v3,v4,v5}){
++counter[v];
}
std::cout<<"V1 is present " <<counter[v1]<<" times.\n";
return 0;
}
V1 is present 2 times.
Well, this is a contribution for Quimby answer, but if you know how many vectors you will get at compile time, use std::array to contain all that vector. If you know it at runtime instead, use std::vector as shown below
#include <map>
#include <vector>
#include <iostream>
#include <cstddef>
int main(){
std::vector<std::vector<int>> allVector
{
std::vector<int>{0,0,1,2,3,0,0,0},
std::vector<int>{0,0,1,2,3,4,0,0},
std::vector<int>{0,0,2,4,3,0,0,0},
std::vector<int>{0,0,1,2,3,0,0,0},
std::vector<int>{0,0,6,2,3,5,6,0},
};
std::map<std::vector<int>, std::size_t> counter;
for(const auto& v : allVector)
{
++counter[v];
}
// print out the array and it's frequency
for(const auto& pr : counter)
{
std::cout << '(';
for(std::size_t i {0}; i < pr.first.size(); ++i)
{
std::cout << pr.first[i];
if(i != pr.first.size() - 1)
std::cout << ' ';
}
std::cout << ") = " << pr.second << ", ";
}
return 0;
}

Cannot predetermine values for a vector

I'm using sublime text for C++ and, for some reason, I am not able to predetermine values for a vector.
std::vector<int> v = {1,2,3,4,5};
Whenever I do such a thing, I get this error:
'std::vector<int>' cannot be initialized with an initializer list
std::vector<int> v = {1,2,3,4,5};
^ ~~~~~~~~~~~
Maybe try this : std::vector v{1,2,3,4,5}; ?
Maybe try this, please
#include <iostream> // std::cout
#include <numeric> // std::iota
#include <vector> // std::vector
// Driver code
int main()
{
std::vector<int> v(6);
std::iota(v.begin(), v.end(), 1);
std::cout << "Elements are :";
for (auto i : v)
std::cout << ' ' << i;
std::cout << '\n';
return 0;
}
Output:
Elements are : 1 2 3 4 5 6
This directly answers your question:
std::vector<int> v;
for(int i=0; i<6; i++)
{
v.push_back(i);
}
To be more generic, checkout:
int myints[] = {16,2,77,29};
std::vector<int> v (myints, myints + sizeof(myints) / sizeof(int) );
Finally print the results with an iterator:
std::vector<int>::iterator vi;
for(vi = v.begin(); vi != v.end(); ++vi)
std::cout << *vi;
One more option would be to push_back() every different value you want.

swap array values in c++

I want to shift left array values if my v=4 is in a[n],remove 4 from a[n] and at the end index add 0,how i can do this?
#include <iostream>
using namespace std;
const int n=5;
int main()
{
int a[n]={1,5,4,6,8}, v=4;
int b[n];
cout << "Enter a Value" << endl;
cout<<v<<endl;
for(int i=0; i<n; i++){
cout<<a[i];
}
cout<<endl;
for(int j=0; j<n; j++){
b[j]=a[j];
if(a[j]==v)
b[j]=a[++j];
cout<<b[j];
}
return 0;
}
#include <vector> // needed for vector
#include <algorithm> // needed for find
#include <iostream> // needed for cout, cin
using namespace std;
// Vectors are just like dynamic arrays, you can resize vectors on the fly
vector<int> a { 1,5,4,6,8 }; // Prepare required vector
int v;
cout << "enter value"; // Read from user
cin >> v;
auto itr = find( a.begin(), a.end(), v); // Search entire vector for 'v'
if( itr != a.end() ) // If value entered by user is found in vector
{
a.erase(itr); // Delete the element and shift everything after element
// Toward beginning of vector. This reduces vector size by 1
a.push_back(0); // Add 0 in the end. This increases vector size by 1
}
for( int i : a ) // Iterate through all element of a (i holds element)
cout << i; // Print i
cout << '\n'; // Line end
a few helpful links:
vector , find , iterator , erase , push_back
You could use std::rotate. I suggest that you use std::vector instead of C arrays and take full advantage of the STL algorithms. Nevertheless, below I'm illustrating two versions one with C arrays and one with std::vector:
Version with C array:
#include <iostream>
#include <algorithm>
int main()
{
int const n = 5;
int a[n] = {1,5,4,6,8};
std::cout << "Enter a Value" << std::endl;
int v;
std::cin >> v;
for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;
auto it = std::find(std::begin(a), std::end(a), v);
if(it != std::end(a)) {
std::rotate(it + 1, it, std::end(a));
a[n - 1] = 0;
}
for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;
return 0;
}
Version with vector:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> a{1,5,4,6,8};
std::cout << "Enter a Value" << std::endl;
int v;
std::cin >> v;
for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;
auto it = std::find(std::begin(a), std::end(a), v);
if(it != std::end(a)) {
std::rotate(it + 1, it, std::end(a));
a.back() = 0;
}
for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;
return 0;
}
Here's an example using std::array
#include <array>
#include <algorithm>
// defines our array.
std::array<int, 5> a = {{ 1, 2, 3, 4, 5 }};
// find the position of the element with the value 4.
auto where = std::find(a.begin(), a.end(), 4);
// if it wasn't found, give up
if (where == a.end())
return 0;
// move every element past "where" down one.
std::move(where + 1, a.end(), where);
// fill the very last element of the array with zero
a[ a.size() - 1] = 0;
// loop over our array, printing it to stdout
for (int i : a)
std::cout << i << " ";
std::cout << "\n";
Why would anyone use these awkward algorithms? Well, there are a few reasons. Firstly, they are container-independant. This will work with arrays and vectors and deques, no problem. Secondly, they can be easily used to work with a whole range of elements at once, not just single items, and can copy between containers and so on. They're also type-independant... you acn have an array of strings, or an vector of ints, or other more complex things, and the algorithms will still work just fine.
They're quite powerful, once you've got over their initial user-unfriendliness.
You can always use either std::array or std::vector or whatever without using the standard library algorithms, of course.
std::array<int, 5> a = {{ 1, 2, 3, 4, 5 }};
size_t where = 0;
int to_remove = 4;
// scan through until we find our value.
while (a[where] != to_remove && where < a.size())
where++;
// if we didn't find it, give up
if (where == a.size())
return 0;
// shuffle down the values
for (size_t i = where; i < a.size() - 1; i++)
a[i] = a[i + 1];
// set the last element to zero
a[ a.size() - 1] = 0;
As a final example, you can use memmove (as suggested by BLUEPIXY) to do the shuffling-down operation in one function call:
#include <cstring>
if (where < a.size() - 1)
memmove(&a[where], &a[where + 1], a.size() - where);

how can I find repeated elements in a vector [duplicate]

This question already has answers here:
Checking for duplicates in a vector [duplicate]
(5 answers)
Closed 9 years ago.
I have a vector of int which can include maximum 4 elements and minimum 2, for example :
std::vector<int> vectorDATA(X); // x means unknown here
What I want to do is to erase the elements that are repeated for example :
vectorDATA{1,2,2} to vectorDATA{1,2}
vectorDATA{1,2,3} to nothing changes
vectorDATA{2,2,2} to vectorDATA{2}
vectorDATA{3,2,1,3} to vectorDATA{3,2,1}
vectorDATA{1,2,1,2} to vector{1,2}
and so on
here a code simple :
cv::HoughLines(canny,lineQ,1,CV_PI/180,200);
std::cout << " line Size "<<lineQ.size()<< std::endl;
std::vector<int> linesData(lineQ.size());
std::vector<int> ::iterator it;
if(lineQ.size() <=4 && lineQ.size() !=0 ){
if(lineQ.size()==1){
break;
}else {
for ( int i = 0; i<lineQ.size();i++){
linesData[i] = lineQ[i][1]; // my comparison parameter is the lineQ[i][1]
}
// based on the answer I got I'm trying this but I really don't how to continue ?
std::sort(lineQ.begin(),lineQ.end(),[](const cv::Vec2f &a,const cv::Vec2f &b)
{
return ????
}
I tried use a for and do while loop, but I didn't get it, and the function std::adjacent_find this has a condition that the elements should be consecutive.
Maybe it's easy but I just don't get it !
thanks for any help !
The easy way is sort then unique-erase, but this changes order.
The c++11 order preserving way is to create an unordered_set<int> s; and do:
unordered_set<int> s;
vec.erase(
std::remove_if( vec.begin(),vec.end(), // remove from vector
[&](int x)->bool{
return !std::get<1>(s.insert(x)); // true iff the item was already in the set
}
),
vec.end() // erase from the end of kept elements to the end of the `vec`
);
which is the remove-erase idiom using the unordered_set to detect duplicates.
I didn't see a sort-less source code in the already mentioned answers, so here it goes. Hash table for checking duplicates, shifting unique elements towards the front of the vector, note that src is always >= dst and dst is the number of copied, i.e. unique elements at the end.
#include <unordered_set>
#include <vector>
#include <iostream>
void
uniq (std::vector<int> &a) {
std::unordered_set<int> s;
size_t dst = 0;
for (size_t src = 0; src < a.size(); ++src) {
if (s.count (a[src]) == 0) {
s.insert (a[src]);
a[dst++] = a[src];
}
}
a.resize (dst);
}
int
main () {
std::vector<int> a = { 3, 2, 1, 3, 2, 1, 2, 3, 4, 5 ,2, 3, 1, 1 };
uniq (a);
for (auto v : a)
std::cout<< v << " ";
std::cout << std::endl;
}
If you want to realy remove repeated elements, you may try something like this:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int data[] = {1,2,3,2,1};
vector<int> vectorDATA = (&data[0], &data[0] + 5);
sort(vectorDATA.begin(),vectorDATA.end());
for(int i = 0; i < vectorDATA.size()-1; ++i)
{
if(vectorDATA[i] == vectorDATA[i+1])
vectorDATA.erase(vectorDATA.begin()+i+1);
}
for(int i = 0; i < vectorDATA.size();++i)
{
cout << vectorDATA[i] << " ";
}
cout << endl;
return 0;
}
Lack of of this method is then elements lost his order.

String vector's sort compare and delete

In C++ I have
vector < vector <string> > Kblist;
Inside Kblist, there are many clause, and numbers of clauses=kblist.size(); and every clause in side Kblist is a string-type vector, and every word in sentence is split inside Kblist[i].
What is the fastest way to find the sentence in same words like one in "I love you" and the other in "you love i" and delete these two sentence from Kblist, My code might be work run, but I think it is too slow because of to many circulate. So I wonder is there any better solution which is fast like using sort, clause1==clause2 or other approach.
for (int a=0; a<KBlist.size(); a++){
for (int b=a+1; b<KBlist.size(); b++){
int checksize=0;
if (KBlist[a].size()==KBlist[b].size()) {
for (int c=0; c<KBlist[a].size(); c++){
for (int d=0; d<KBlist[b].size(); d++){
if (KBlist[a][b]==KBlist[c][d]&&KBlist[a][b+1]==KBlist[c][d]) {
checksize=checksize+1;
break;
}
}
}
if (checksize==c.size()) {
inset=1;
break;
}
}
}
}
}while (duplicate==0);
You could iterate over each std::vector and use the algorithms of the standard-library.
There is std::find
// find example
#include <iostream> // std::cout
#include <algorithm> // std::find
#include <vector> // std::vector
int main () {
int myints[] = { 10, 20, 30 ,40 };
int * p;
// pointer to array element:
p = std::find (myints,myints+4,30);
++p;
std::cout << "The element following 30 is " << *p << '\n';
std::vector<int> myvector (myints,myints+4);
std::vector<int>::iterator it;
// iterator to vector element:
it = find (myvector.begin(), myvector.end(), 30);
++it;
std::cout << "The element following 30 is " << *it << '\n';
return 0;
}
there is std::find_if
// find_if example
#include <iostream> // std::cout
#include <algorithm> // std::find_if
#include <vector> // std::vector
bool IsOdd (int i) {
return ((i%2)==1);
}
int main () {
std::vector<int> myvector;
myvector.push_back(10);
myvector.push_back(25);
myvector.push_back(40);
myvector.push_back(55);
std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
std::cout << "The first odd value is " << *it << '\n';
return 0;
}
As you are working with std::string this shouldn't be a large problem.
In your scenario it is probably better to use std::multiset< vector <string> > with comparator that compares std::vector<string> in a way you need it to. This will give you sorted container with duplicated values next to each other and cheap insert/erase.