I just downloaded the STL source code and I noticed all the definition for the STL template classes are included in the .h file. The actual source code for the function definition is in the .h file rather than .cpp/.c file. What is the reason for this?
http://www.sgi.com/tech/stl/download.html
...
I would like to be able to convert between std::vector and its underlying C array int* without explicitly copying the data.
Does std::vector provide access to the underlying C array? I am looking for something like this
vector<int> v (4,100)
int* pv = v.c_array();
EDIT:
Also, is it possible to do the converse, i.e. how would I ini...
inline void add(const DataStruct& rhs) {
using namespace boost::assign;
vec.reserve(vec.size() + 3);
vec += rhs.a, rhs.b, rhs.c;
}
The above function was executed for about 17000 times and it performed (as far as I can see. There was some transformation involved) about 2 magnitudes worse with the call to vector::reserve.
I ...
vector< vector<int> >::iterator temp = mincost.end();
vector<int> a = *temp;
if ( *temp != *(temp--) )
return 0;
mincost is a 2d vector, I want to get the last vector<int> of this vector and last--.
I don't really understand about iterator :) . Help me !! :D
Thx ^^
...
This works fine:
std::vector<int> v;
v.push_back(123);
but this throws a std::length_error:
std::vector<uint32_t> v;// or vector<unsigned __int32>
v.push_back(123);
It seems to be triggered by resizing, because
std::vector<uint32_t> v;
v.reserve(2);
triggers a debug assertion "iterator not dereferencable".
This occu...
Hi,
This is similar to a recent question.
I will be maintaining sorted a list of values. I will be inserting items of arbitrary value into the list. Each time I insert a value, I would like to determine its ordinal position in the list (is it 1st, 2nd, 1000th). What is the most efficient data structure and algorithm for accomplishing...
Hi all,
I have a large matrix M implemented as vector<vector<double> with m rows, i.e. the matrix is a vector of m vectors of n column elements.
I have to create two subsets of the rows of this matrix, i.e. A holds k rows, and B the other m-k rows. The rows must be selected at random.
I do not want to use any libraries other than STL...
Consider the following code:
typedef std::vector<int> cont_t; // Any container with RA-iterators
typedef cont_t::const_iterator citer_t; // Random access iterator
cont_t v(100);
const int start = 15; // start > 0.
citer_t it = v.begin() - start; // Do not use *it
int a1 = 20, b1 = 30; // a1, b1 >= start
int a2 = 30, b2 = 40; // a2, b2...
For a specific example,consider atoi(const std::string& ).I am very frustrated about this,since we programmers would need to use it so much.
More general question is why does not C++ standard library reimplement the standard C libraries with C++ string,C++ vector or other C++ standard element rather than to preserve the old C standard ...
When a C++ function accepts an std::vector argument, the usual pattern is to pass it by const reference, such as:
int sum2(const std::vector<int> &v)
{
int s = 0;
for(size_t i = 0; i < v.size(); i++) s += fn(v[i]);
return s;
}
I believe that this code results in double dereferencing when the vector elements are accessed, beca...
The standard way of intersecting two sets in C++ is to do the following:
std::set<int> set_1; // With some elements
std::set<int> set_2; // With some other elements
std::set<int> the_intersection; // Destination of intersect
std::set_intersection(set_1.begin(), set_1.end(), set_2.begin(), set_2.end(), std::inserter(the_intersection, ...
So you have this simple program that creates a set from a file :
#include <fstream>
#include <string>
#include <set>
int main()
{
std::set<std::string> Sdictionnary;
std::set<std::string>::const_iterator it = Sdictionnary.begin();
std::ifstream file("french.txt"); // A file containing 200 000 lines
std::string line;
while(getline...
I am new to C++ and I have some confusion regarding this program. I am running this in Visual Studio 2008 as win32 console application.
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
#define PI 3.14
int l=1;
int x;
void main()
{
do
{
cout << "cho...
Hi all,
I am unable to detect the error in the following C++ program.
The code defines a Graph class.
#include <vector>
#include <list>
using namespace std;
class Neighbor {
public:
int node_id;
float edge_cost;
float price;
Neighbor(int&,float&,float&);
};
Neighbor::Neighbor(int& node_id, float& edge_cost,float& price) {
...
How to create iterator/s for 2d vector (a vector of vectors)?
...
I have class A, that contains std::vector and I would like to give an access to the vector from outside class A.
The first thing that came to my mind is to make a get function that returns iterator to the vector, but walk through the vector I will need two iterators (begin and end).
I was wondering is there any way (technique or patter...
Looks like a stupid question. But comment to my answer to one of the SO question made me to think again.
[ comment says, capacity need not be zero for empty vector]
By default my answer would be 0 as there are no elements inside vector. It makes sense to keep the capacity as 0 and on the first allocation it can be increased without an...
I'm trying to use STL to solve the following problem (I don't want to implement my own data structure if I don't have to). I've come up with a working implementation, but I'm hoping there's something faster...or is what code I have the best way to do it?
I have a large data set in which each entry contains two items: a key and a size. ...
I am trying to lean boost::bind, boost::lambda libraries and how they can be used with STL algorithms. Suppose I have vector of int-string pairs which is sorted by int key. Then a place to insert a new pair while keeping the vector sorted can be found as follows:
std::vector<std::pair<int, string> > entries;
...
int k = ...;
// Let's i...
Can I construct an std::map where the key type is a reference type, e.g. Foo & and if not, why not?
...