C++/Vector/vector size
Содержание
Computing the sum with template iterators
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
template <typename Iter>
double vectorSum(Iter begin, Iter end) {
double sum = 0.0;
while( begin != end )
sum += *begin++;
return sum;
}
int main() {
vector<int> sunny;
sunny.push_back(7);
sunny.push_back(12);
sunny.push_back(15);
cout << sunny.size() << " months on record" << endl;
cout << "vectorSum number of sunny days: ";
cout << vectorSum(sunny.begin(), sunny.end()) << endl;
return 0;
}
/*
3 months on record
vectorSum number of sunny days: 34
*/
Make vector large enough to hold all value
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
class PC
{
public:
enum part { keyboard, mouse, monitor };
PC( part a_part = PC::keyboard, int id = 0 );
bool operator<( const PC& rhs ) const;
void print() const;
private:
part part_;
int id_;
};
inline
PC::PC( part a_part, int id ) : part_( a_part ), id_( id ){}
inline bool PC::operator<( const PC& rhs ) const{
return id_ < rhs.id_;
}
void PC::print() const {
string component;
if( part_ == keyboard )
component = "keyboard";
else if( part_ == mouse )
component = "mouse";
else
component = "monitor";
cout << "ID: " << setw( 8 ) << left << id_ << " PC: " << component << endl;
}
int main( )
{
list<PC> listA;
listA.push_back( PC( PC::keyboard, 3 ) );
listA.push_back( PC( PC::mouse, 1 ) );
listA.push_back( PC( PC::monitor, 9 ) );
listA.push_back( PC( PC::keyboard, 2 ) );
listA.push_back( PC( PC::monitor, 8 ) );
list<PC> inspector_B( listA );
inspector_B.front() = PC( PC::mouse, 6 );
inspector_B.back() = PC( PC::monitor, 1 );
// must sort before using set algorithms
listA.sort();
inspector_B.sort();
vector<PC> result;
result.resize( listA.size() + inspector_B.size() );
}
vector: max_size(), size(), capacity()
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
// create empty vector for strings
vector<string> sentence;
// reserve memory for five elements to avoid reallocation
sentence.reserve(5);
// append some elements
sentence.push_back("Hello,");
sentence.push_back("how");
sentence.push_back("are");
sentence.push_back("you");
sentence.push_back("?");
// print elements separated with spaces
copy (sentence.begin(), sentence.end(),
ostream_iterator<string>(cout," "));
cout << endl;
cout << " max_size(): " << sentence.max_size() << endl;
cout << " size(): " << sentence.size() << endl;
cout << " capacity(): " << sentence.capacity() << endl;
}
/*
Hello, how are you ?
max_size(): 1073741823
size(): 5
capacity(): 5
*/
vector size before and after elements insertion
#include <iostream>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
vector<char> v(10); // create a vector of length 10
cout << "Size = " << v.size() << endl;
for(int i=0; i<10; i++)
v[i] = i + "a";
cout << "Current Contents:\n";
for(int i=0; i<v.size(); i++) cout << v[i] << " ";
cout << "\n\n";
cout << "Expanding vector\n";
for(int i=0; i<10; i++)
v.push_back(i + 10 + "a");
cout << "Size now = " << v.size() << endl;
cout << "Current contents:\n";
for(int i=0; i<v.size(); i++)
cout << v[i] << " ";
cout << "\n\n";
return 0;
}
/*
Size = 10
Current Contents:
a b c d e f g h i j
Expanding vector
Size now = 20
Current contents:
a b c d e f g h i j k l m n o p q r s t
*/