459,912 Members | 1,710 Online
Need help? Post your question and get tips & solutions from a community of 459,912 IT Pros & Developers. It's quick & easy.

# how do u convert a vector<float> A to a vector<double> B

 P: n/a and why doesnt the standard vector have such conversion available? Aug 31 '05 #1
10 Replies

 P: n/a wrote in message news:11**********************@o13g2000cwo.googlegr oups.com... and why doesnt the standard vector have such conversion available? You can use the vector constructor that takes two iterators: #include #include using namespace std; int main() { vector a; a.push_back(1.2f); vector b(a.begin(), a.end()); assert(!b.empty()); cout << b.front() << '\n'; } Ali Aug 31 '05 #2

 P: n/a > Re: how do u convert a vector A to a vector B std::vector A; /* etc */ std::vector B(A.begin(), A.end()) wrote in message news:11**********************@o13g2000cwo.googlegr oups.com... and why doesnt the standard vector have such conversion available? It does. See above. -Mike Aug 31 '05 #3

 P: n/a What if b is already constructed? Another question Say you have vector a(3); //init a vector > c(3); //init c Now I want something like real(c)=a; Currently I have for(int i=0;i

 P: n/a wrote in message news:11**********************@o13g2000cwo.googlegr oups.com... What if b is already constructed? Then it's assignment: b = vector(a.begin(), a.end()); Another question Say you have vector a(3); //init a vector > c(3); //init c Now I want something like real(c)=a; Currently I have for(int i=0;i Reals; typedef vector > Complexes; void set_reals(Complexes & complexes, Reals const & reals) { // some checks if (complexes.size() != reals.size()) { throw SomeError; } /* the logic here */ } Very neat :) More cool (and possibly more obscure) things can probably be done, but I really don't think that it's worth it. Ali Aug 31 '05 #5

 P: n/a "Ali Çehreli" wrote in message news:df**********@domitilla.aioe.org... wrote in message news:11**********************@o13g2000cwo.googlegr oups.com... What if b is already constructed? Then it's assignment: b = vector(a.begin(), a.end()); Actually, a probably better expression (for clarity, maintainability, and performance) is: b.assign( a.begin(), a.end() ); hth, Ivan -- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form Brainbench MVP for C++ <> http://www.brainbench.com Sep 1 '05 #6

 P: n/a On 2005-08-31 18:14:04 -0400, bl**********@gmail.com said: and why doesnt the standard vector have such conversion available? #include #include //for std::copy .... std::vector A = ...; //Construct B with the contents of A std::vector B(A.begin(), A.end()); //Or if B is already constructed: B.clear(); B.insert(B.begin(), A.begin(), A.end()); //Or: B.resize(A.size()); std::copy(A.begin(), A.end(), B.begin()); -- Clark S. Cox, III cl*******@gmail.com Sep 1 '05 #7

 P: n/a On 2005-09-01 00:09:55 -0400, "Ivan Vecerina" said: "Ali Çehreli" wrote in message news:df**********@domitilla.aioe.org... wrote in message news:11**********************@o13g2000cwo.googlegr oups.com... What if b is already constructed? Then it's assignment: b = vector(a.begin(), a.end()); Actually, a probably better expression (for clarity, maintainability, and performance) is: b.assign( a.begin(), a.end() ); There is no "assign" function on std::vector. -- Clark S. Cox, III cl*******@gmail.com Sep 1 '05 #8

 P: n/a Clark S. Cox III wrote: Actually, a probably better expression (for clarity, maintainability, and performance) is: b.assign( a.begin(), a.end() ); There is no "assign" function on std::vector. 23.2.4.1 vectors constructors, copy, and assignment template void assign(InputIterator first, InputIterator last); Effects: erase(begin(), end()); insert(begin(), first, last); template void assign(Size n, const U& u = U()); Effects: erase(begin(), end()); insert(begin(), n, t); Maybe your compiler lacks member template support? Marc Sep 1 '05 #9

 P: n/a Clark S. Cox III wrote: //Or: B.resize(A.size()); std::copy(A.begin(), A.end(), B.begin()); b.clear(); b.reserve( a.size() ); std::copy( a.begin(), a.end(), std::back_inserter( b ) ); Marc Sep 1 '05 #10

 P: n/a On 2005-09-01 08:52:49 -0400, Marc Mutz said: Clark S. Cox III wrote: Actually, a probably better expression (for clarity, maintainability, and performance) is: b.assign( a.begin(), a.end() ); There is no "assign" function on std::vector. 23.2.4.1 vectors constructors, copy, and assignment template void assign(InputIterator first, InputIterator last); Effects: erase(begin(), end()); insert(begin(), first, last); template void assign(Size n, const U& u = U()); Effects: erase(begin(), end()); insert(begin(), n, t); Maybe your compiler lacks member template support? Well, I'll be. I can't believe I missed that. I think I was looking at the first listing in 23.2.4 without remembering the "Descriptions are provided here only for operations on vector that are not described in one of these tables or for operations where there is additional semantic information." part. -- Clark S. Cox, III cl*******@gmail.com Sep 1 '05 #11

### This discussion thread is closed

Replies have been disabled for this discussion.