Skip to content

Commit 6eb800b

Browse files
committed
insertAt missing vec_size++
1 parent f6e6ef5 commit 6eb800b

File tree

3 files changed

+66
-73
lines changed

3 files changed

+66
-73
lines changed

p10b/hwk3/VectorString.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1+
12
/**
23
@file VectorString.cpp
3-
@author Radhika Nayar
4-
@brief This is the cpp file for my own VectorString class for Assignment3 Pic 10B
5-
// Created by Radhika Nayar on 4/18/19.
6-
// Copyright © 2019 Radhika Nayar. All rights reserved.
7-
*/
4+
*/
85

96
#include "VectorString.h"
107

118
namespace pic10b{ // open up the namespace
12-
9+
1310
// Default Constructor: size is 0, capacity is 1 (initial_capacity), ptr points to dynamic array of string of one element
1411
VectorString::VectorString(): vec_size(0), vec_capacity(0){
1512
reserve(INITIAL_CAPACITY);
1613
}
17-
14+
1815

1916
// Constructor2: accepts size (count) and input string (value).
2017
// Allocates a dynamic array with size as count, capacity as twice count and initializes all string variables to value.
@@ -85,11 +82,11 @@ namespace pic10b{ // open up the namespace
8582
//data_ = new_data; // data_ now points to the new memory location
8683
vec_capacity = new_cap; // vec_capacity is the new capacity
8784
}
88-
85+
8986
bool VectorString::empty() const{
9087
return (size()==0);
9188
}
92-
89+
9390
// Size member function: returns the size of the vector.
9491
size_type VectorString::size() const{
9592
return vec_size;
@@ -117,7 +114,7 @@ namespace pic10b{ // open up the namespace
117114
vec_size--; // deletes one element as long as size is not zero
118115
}
119116
}
120-
117+
121118
// DeleteAt member function: accepts an index value that removes the element at the given index and shifts all elements backwards.
122119
void VectorString::deleteAt(size_type pos){
123120
if(pos>=0 && pos<size()){
@@ -140,17 +137,17 @@ namespace pic10b{ // open up the namespace
140137
data_[index] = data_[index-1];
141138
}
142139
data_[pos]= value; // sets value to pos index
140+
vec_size++;
143141
}
144-
142+
145143

146144
// At member function: Accepts an index value that returns the element at the given index by reference or reference to const.
147145
std::string& VectorString::at( size_type pos ) {
148146
return data_[pos]; // gives value at pos
149147
}
150-
148+
151149
// At member function: Accepts an index value that returns the element at the given index by reference or reference to const.
152150
const std::string& VectorString::at( size_type pos ) const{
153151
return data_[pos]; // gives value at pos
154152
}
155153
}
156-

p10b/hwk3/VectorString.h

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,112 +24,112 @@ namespace pic10b{ // declaring namespace pic10b
2424
public:
2525

2626
/**
27-
Default Constructor: size is 0, capacity is 1 (initial_capacity), ptr points to dynamic array of string of one element
27+
Default Constructor: size is 0, capacity is 1 (initial_capacity), ptr points to dynamic array of string of one element
2828
*/
2929
VectorString();
3030

3131
/**
32-
Constructor2: Allocates a dynamic array with size as count, capacity as twice count and initializes all string variables to value.
33-
@param count size of the vector
34-
@param value string value
35-
*/
32+
Constructor2: Allocates a dynamic array with size as count, capacity as twice count and initializes all string variables to value.
33+
@param count size of the vector
34+
@param value string value
35+
*/
3636
VectorString(size_type count, const std::string& value);
3737

3838
/** Constructor3: Allocates a dynamic array with size as count, capacity as twice count and initializes all string variables to empty string.
39-
@param count size of the vector
40-
*/
39+
@param count size of the vector
40+
*/
4141
VectorString(size_type count);
42-
42+
4343
/**
44-
Copy constructor: makes a new independent copy of the other VectorString
45-
@param other the VectorString to copy from
46-
*/
44+
Copy constructor: makes a new independent copy of the other VectorString
45+
@param other the VectorString to copy from
46+
*/
4747
VectorString(const VectorString& other);
4848

4949
/**
50-
Move constructor: takes data from an rvalue of type VectorString
51-
@param other the VectorString reaching the end of its lifetime
52-
*/
50+
Move constructor: takes data from an rvalue of type VectorString
51+
@param other the VectorString reaching the end of its lifetime
52+
*/
5353
VectorString(VectorString&& other);
5454

5555
/**
56-
Copy assignment operator, makes left value same as right value
57-
@param other the assigned-from VectorString
58-
@return the updated assigned-to VectorString
59-
*/
56+
Copy assignment operator, makes left value same as right value
57+
@param other the assigned-from VectorString
58+
@return the updated assigned-to VectorString
59+
*/
6060
VectorString& operator=(const VectorString& other);
6161

6262
/**
63-
Move assignment operator, makes left value same as right value by harvesting its resources
64-
@param other the assigned-from VectorString
65-
@return the updated assigned-to VectorString
66-
*/
63+
Move assignment operator, makes left value same as right value by harvesting its resources
64+
@param other the assigned-from VectorString
65+
@return the updated assigned-to VectorString
66+
*/
6767
VectorString& operator=(VectorString&& other);
6868

6969
/**
70-
Checks if the container has no elements, i.e. whether begin() == end()
71-
@return true if the container is empty, false otherwise
72-
*/
70+
Checks if the container has no elements, i.e. whether begin() == end()
71+
@return true if the container is empty, false otherwise
72+
*/
7373
bool empty() const;
7474

7575
/**
76-
Size member function, returns the size of the vector
77-
@return size of the vector
78-
*/
76+
Size member function, returns the size of the vector
77+
@return size of the vector
78+
*/
7979
size_type size() const;
8080

8181
/**
82-
Capacity member function, returns the capacity of the vector
83-
@return capacity of the vector
84-
*/
82+
Capacity member function, returns the capacity of the vector
83+
@return capacity of the vector
84+
*/
8585
size_type capacity() const;
8686

8787
/**
8888
Reserve member function, increases the capacity of the vector to a value that's greater or equal to new_cap. If new_cap is greater than the current capacity(), new storage is allocated, otherwise the method does nothing.
89-
@param new_cap new capacity inputted
90-
*/
89+
@param new_cap new capacity inputted
90+
*/
9191
void reserve(size_type new_cap);
9292

9393
/**
9494
Push Back member function, adds an element to the end of the vector if the capacity allows. Otherwise creates a new dynamic array of twice the former capacity - moving all the elements over and adding the new element.
95-
@param value is the string value
96-
*/
95+
@param value is the string value
96+
*/
9797
void push_back(const std::string& value);
98-
98+
9999
/**
100-
Pop Back member function, removes the last element of the vector by updating the size of the vector
101-
*/
100+
Pop Back member function, removes the last element of the vector by updating the size of the vector
101+
*/
102102
void pop_back();
103103

104104
/**
105-
DeleteAt member function: accepts an index value that removes the element at the given index and shifts all elements backwards.
106-
@param pos is the position at which the string value should be deleted
105+
DeleteAt member function: accepts an index value that removes the element at the given index and shifts all elements backwards.
106+
@param pos is the position at which the string value should be deleted
107107
*/
108108
void deleteAt (size_type pos);
109109

110110
/**
111-
InsertAt member function, adds an element to the pos of the vector if the capacity allows. Otherwise creates a new dynamic array of twice the former capacity - moving all the elements over and adding the new element at pos
112-
@param pos is the position at which the value string should be inserted
113-
@param value is the string value
114-
*/
111+
InsertAt member function, adds an element to the pos of the vector if the capacity allows. Otherwise creates a new dynamic array of twice the former capacity - moving all the elements over and adding the new element at pos
112+
@param pos is the position at which the value string should be inserted
113+
@param value is the string value
114+
*/
115115
void insertAt (size_type pos, std::string value);
116116

117117
/**
118-
At member function: return by reference, the value at the position index
119-
@param pos is the position at which the value of string is returned
120-
@return value of the string at position pos
121-
*/
118+
At member function: return by reference, the value at the position index
119+
@param pos is the position at which the value of string is returned
120+
@return value of the string at position pos
121+
*/
122122
std::string& at(size_type pos);
123123

124124
/**
125-
At member function overloaded on const: return by reference to const, the value at the position index
126-
@param pos is the position at which the value of string is returned
127-
@return value of the string at position pos
128-
*/
125+
At member function overloaded on const: return by reference to const, the value at the position index
126+
@param pos is the position at which the value of string is returned
127+
@return value of the string at position pos
128+
*/
129129
const std::string& at(size_type pos) const;
130-
130+
131131
private:
132-
132+
133133
size_type vec_size; // size of the vector
134134
size_type vec_capacity; // capacity of the vector
135135
std::unique_ptr<std::string[]> data_; // pointer to the dynamic array of string vector, defaulted to nullptr

p10b/hwk3/main.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
//
22
// main.cpp
3-
// ASSIGNMENT3 PIC10B
4-
//
5-
// Created by Radhika Nayar on 4/18/19.
6-
// Copyright © 2019 Radhika Nayar. All rights reserved.
73
//
84

95
#include <iostream>
@@ -15,14 +11,14 @@ pic10b::VectorString foo() {
1511
}
1612

1713
int main() {
18-
14+
1915
pic10b::VectorString vs;
2016
std::cout << "vs stats: " << vs.size() << " " << vs.empty() << " " << vs.capacity() << '\n';
2117
vs.push_back("hello");
2218
std::cout << "vs stores: " << vs.at(0) << '\n';
2319

2420
auto foo_out = foo();
25-
21+
2622
pic10b::VectorString vs2(std::move(foo_out));
2723
std::cout << "vs2 stats: " << vs2.size() << " " << vs2.capacity() << '\n';
2824

0 commit comments

Comments
 (0)