3

So I have a 2D Vector here that I want to assign a value num, I want to see which performs better fill vs memset() as C++ noob, I am actually having problem setting the proper code syntax as I just always get Segmentation fault when I do it.

vector<vector<int>> matrix(10, vector<int>(10000000));
int main()
{
    int num;
    cin >> num;
    int i = 0;
    for (auto &i : matrix)
    {
        fill(i.begin(), i.end(), num); 
    }
    return 0;
}
Zambodia
  • 41
  • 1
  • 2
  • perfom better in terms of what? In terms of not confusing the reader of your code `memset` isnt that nice, because there is no obvious reason to use it ;) – 463035818_is_not_an_ai Apr 19 '18 at 09:14
  • 2
    Show us your memset attempt? – Mike Vine Apr 19 '18 at 09:14
  • Not diretly related to your question, but the `int i = 0;` declaration is pointless, you can remove it alltogether. – Jabberwocky Apr 19 '18 at 09:17
  • 4
    You can't `memset` to arbitary `int`s. – molbdnilo Apr 19 '18 at 09:19
  • memset works on bytes, if you use memset with with `1` you'll get ints of value 16843009, see [this example on ideone](https://ideone.com/VrdDov) – PeterT Apr 19 '18 at 09:37
  • Thanks for the support guys, very new at this, for my memset attempt memset(&matrix[0], num, sizeof(matrix[0]) * matrix.size()); I'm pretty sure I used it wrongly though. – Zambodia Apr 19 '18 at 09:53
  • @Zambodia -- Your usage of `memset` will not work as you think it will work, so forget about it. Even if it were not `vector` and was a regular array, `memset` will not work as intended. – PaulMcKenzie Apr 19 '18 at 10:04
  • @PaulMcKenzie thanks for pointing that out, I guess you already understood what I wanted to do and clearly stated that it might really be impossible. – Zambodia Apr 19 '18 at 10:09

1 Answers1

5

You can use memset for std::vector<int>, I don't think it's be such a good idea and it's fairly ugly with std::vector. Yes you can use std::fill the way you using it but there is a simpler way in your case, use std::vector constructor. Like this :

int main()
{
    int num;
    std::cin >> num;
    std::vector<std::vector<int>> matrix(10, std::vector<int>(10000000, num));
    return 0;
}
HMD
  • 2,202
  • 6
  • 24
  • 37
  • In case that the right most dimension is **always** fixed, one can have all those items as a [ContiguousContainer](http://en.cppreference.com/w/cpp/concept) concept like `vector> vv{10, array{num} };` – sandthorn Apr 19 '18 at 09:47
  • Thanks Hamed for the reply, but I'd like to use the case where I need to assign a value(possibly different from each other) for the time being though I have only used a substitute that I assign 1 value to all. – Zambodia Apr 19 '18 at 09:55
  • @Zambodia I'm not sure if I understand you correctly but if you need to fill `std::vector` with different numbers you need to do it step-by-step, like using `push_back` and if there is a sequence that you want to fill your vector with it, I think this is the way to go -> https://stackoverflow.com/questions/17694579/use-stdfill-to-populate-vector-with-increasing-numbers – HMD Apr 19 '18 at 10:02
  • @Hamed, Thanks I will probably use this then. – Zambodia Apr 19 '18 at 10:06